c++中用户名/密码检查器错误
Error with username/password checker in C++
我目前正在尝试学习一些基本的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();
}
由于getline
,您的用户名在第一次尝试后包含换行字符'n'从
更改您的cin使用getline( cin, username, ' ');
cin>>password;
cin >> username;
cin >> password;
修复问题
相关文章:
- error_code:如何设置和检查错误
- CocoaPod - 在头文件中使用 C++ 时出现 Podspec 检查错误
- 在编写包装现有函数并检查错误的模板函数时,如何使用完美转发?
- swscanf_s - 运行时检查错误
- 为什么C++字符串迭代器不检查错误?
- 如何使用运行时检查错误来修复我的C 格斗游戏
- XCode -Mac App -Bootstrap启动时检查错误
- 挂钩发送/WSASEND返回运行时检查错误#0
- 从整数到枚举值时,如何和何处检查错误
- C .如何检查错误的输入
- C++ 输入字符而不是 int 会导致无限循环.如何检查错误的输入
- 检查错误时重载>>运算符中的无限循环
- 开发基本的C++代码+检查错误/警告
- 在C++中,如何在使用gzstream编写压缩文件时检查错误
- 如何使 cuda-memcheck 在出现泄漏检查错误时返回非零
- 是否可以使用GetLastError检查错误代码是否已恢复
- 何时检查错误,使用 QIODevice 的阻塞接口(QTcpSocket 和 QFile)
- 在调用 recv() 之前检查错误
- C++类型检查错误
- 如何修复配置 PHP 7.0.1 时C++预处理器"/lib/cpp"健全性检查错误