【发布时间】:2015-08-05 21:30:43
【问题描述】:
我目前正在尝试学习一些基本的 C++ 编程,并决定让自己成为一个基本的 3 次尝试用户名和密码检查器,以练习我所阅读的一些内容。问题是当我运行程序并首先输入错误的用户名和密码时,如果在第二次或第三次尝试输入时,程序将不再识别正确的用户名和密码。我已经看了很长时间了,似乎无法让它发挥作用。 我什至添加了一条当前注释掉的行,以确保程序正在读取正确的输入,确实如此。
#include <iostream>
#include <string>
using namespace std;
int main()
{
int attempts=0;
string username, password;
while (attempts < 3)
{
cout<<"\nPlease enter your username and password seperated by a space.\n";
getline( cin, username, ' ');
cin>>password;
if (username == "Ryan" && password == "pass")
{
cout<<"\nYou have been granted access.";
return 0;
cin.get();
}
else
{
attempts++;
//cout<<username <<" " <<password << "\n";
cout<<"Incorrect username or password, try again.";
cout<<"\nAttempts remaining: "<<3-attempts <<"\n";
}
}
cout<<"\nOut of attempts, access denied.";
cin.get();
}
非常感谢任何帮助或批评。
【问题讨论】:
-
顺便说一句,
return 0; cin.get();块中的return 0; cin.get();没有意义。return是整个函数的结尾,cin.get();不会被执行。 -
是的,我认为这是从早期阶段或其他一些事情中遗留下来的。感谢您指出这一点。我绝对可以用来养成自己回去打扫卫生的习惯。
标签: c++ password-checker