【发布时间】:2014-03-31 13:21:17
【问题描述】:
您好,这是我第一次使用课程,因此为我糟糕的解释深表歉意。基本上我正在为电梯程序制作密码功能。 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;
}
【问题讨论】:
-
你试过使用调试器吗?
-
您也应该发布您的
LogIn定义,因为您有一些令人困惑的陈述:LogIn Authenticate(name, nameAttempt)vs. LogIn 是我的班级名称,其中包含字符串“john”是密码。name是干什么用的?LogIn::LogIn的参数类型是什么?您是按值复制还是LogIn存储引用?