嗨,这是我第一次使用课程,所以对我糟糕的解释表示歉意。基本上我正在为电梯程序制作密码功能。 LogIn 是我的类的名称,其中包含字符串“john”,它是密码。除了错误密码尝试的循环之外,一切似乎都工作正常。
如果第一次密码尝试正确,则代码可以正常工作,但是如果密码输入不正确,则该行"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;
}
在重试循环中,您仍然需要验证尝试的名称,并在名称被接受时中断循环。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)