嗨,这是我第一次使用类,所以道歉为我的糟糕的解释。基本上我为电梯程序制作密码功能。 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;
}
你有没有尝试过使用调试器?的[得到一个字符串密码功能工作] – Drop
可能重复(http://stackoverflow.com/questions/22610500/getting-a-string-to-work-in-password-function) – Deduplicator
你应该张贴你的'的LogIn '定义,因为你有一些令人困惑的陈述:'LogIn Authenticate(name,nameAttempt)'与* LogIn是我的类的名称,其中包含字符串“john”,它是密码*什么是'name'对于? LogIn :: LogIn的参数类型是什么?你是按值复制还是'LogIn'存储引用? – BeyelerStudios