2014-03-31 340 views
0

嗨,这是我第一次使用类,所以道歉为我的糟糕的解释。基本上我为电梯程序制作密码功能。 LogIn是我的班级的名称,其中包含字符串“john”,它是密码。一切似乎都工作正常,除了密码尝试不正确的循环。密码验证c + +

如果密码尝试是正确的第一次,然后代码workds罚款,但如果输入的密码不正确,那么该行"Incorrect name. Try again"出现在接下来的两次尝试,无论是否密码已输入正确。我希望有人能看到我要出错的地方。 name是存储的密码,并且nameAttempt是用户输入的尝试密码。

#include "stdafx.h" 
#include "LogIn.h" 
#include <iostream> 
#include <iostream> 
#include <string> 

using namespace std; 


bool password() { 

    string name; 
    string nameAttempt; 
    int attempts = 0; 

    cout << "nameAttempt: " << endl; 
    cin >> nameAttempt; 

    LogIn Authenticate(name, nameAttempt); 


    if (Authenticate.getName() == Authenticate.getNameAttempt()) 
    { 
      return true; 
    } 
    else 
      while (Authenticate.getName() != Authenticate.getNameAttempt()) 
      { 
        if (attempts++ ==2) 
        { 
          return false; 
        }  
        cout<<"Incorrect name. Try again"<< endl; 
        cout<< "" << endl; 

        cout << "Enter Name:"<< endl; 
        cin >>nameAttempt; 
      } 
} 



int main() 
{ 

    bool password(); 

    bool loggedin = password(); 

    if(loggedin) { 
     cout << "Password Correct" << endl; 
    } 

    if(!loggedin) { 
     cout << "Incorrect Password" << endl; 
     cout << "Program will now terminate" << endl; 
     system("pause"); 
     return 0; 
    } 

    cout << "you are now free to enter lift" << endl; 

    system("pause"); 
    return 0; 
} 
+0

你有没有尝试过使用调试器?的[得到一个字符串密码功能工作] – Drop

+1

可能重复(http://stackoverflow.com/questions/22610500/getting-a-string-to-work-in-password-function) – Deduplicator

+0

你应该张贴你的'的LogIn '定义,因为你有一些令人困惑的陈述:'LogIn Authenticate(name,nameAttempt)'与* LogIn是我的类的名称,其中包含字符串“john”,它是密码*什么是'name'对于? LogIn :: LogIn的参数类型是什么?你是按值复制还是'LogIn'存储引用? – BeyelerStudios

回答

0

在重试循环中,如果名称被接受,您仍然需要验证尝试的名称并断开循环。

0

您初始化本地函数变量

int attempts = 0; 

所以在while循环退出条件将trigerred第三次代码

if (attempts++ ==2) 

运行,所以你会打印两次:

while (Authenticate.getName() != Authenticate.getNameAttempt()) 
{ 
    if (attempts++ ==2) // increment attempts 
{ 
    return false; 
}  

看起来,这是故意做第二次打印后退出,这样你的困惑是很难明白了。使用调试器,这种错误很容易调查。

0

我认为的代码应该是这样的:

while (1) 
    { 
     if (Authenticate.getName() == Authenticate.getNameAttempt()) 
     { 
      return true; 
     } 
     else   
     { 
      if (attempts++ == 2) 
      { 
       return false; 
      } 
      cout << "Incorrect name. Try again" << endl; 
      cout << "" << endl; 

      cout << "Enter Name:" << endl; 
      cin >> nameAttempt; 

      Authenticate.setNameAttempt(nameAttempt); 
     } 
    } 
0

试试这个,甜蜜和简单的:

cout << "nameAttempt: " << endl; 
cin >> nameAttempt; 

LogIn Authenticate(name, nameAttempt); 
attempts = 0; 
while (attempts<2) 
{ 
    if (Authenticate.getName() == Authenticate.getNameAttempt()) 
    { 
     return true; 
    } 

    cout<<"Incorrect name. Try again"<< endl; 
    cout<< "" << endl; 

    cout << "Enter Name:"<< endl; 
    cin >>nameAttempt; 
    attempts++; 
    LogIn Authenticate(name, nameAttempt); 
} 
return false; 
+0

仍然遇到同样的问题。如果我第一次猜测错误,那么即使输入正确,接下来的两次也是错误的 – user3420131

+0

您能告诉我什么:LogIn Authenticate(name,nameAttempt);呢? – numX

+0

另外,在你的代码中你声明了但没有实例化变量'name' – numX