这可能已经在某个地方得到了解答,但我花了几分钟时间寻找并且找不到任何东西。我有两个文件User.txt
和Pass.txt
。
Pass.txt
看起来像这样:
password
test
User.txt
看起来像这样:
username
test
这是我的代码:
cout << "Username: " << endl; //input user
cin >> user;
cout << "Password: " << endl; //input pass
cin >> pass;
userfile.open("User.txt");
passfile.open("Pass.txt");
while (getline(userfile, STRINGT) && getline(passfile, STRINGO))
{
if (user == STRINGT.c_str() && pass == STRINGO.c_str()) //if user and pass are both in file
{
//i had a break here, (its in a switch) so i replaced it with a cout, but it didn't change
}
}
cout << "Incorrect username or password" << endl;
_getch(); //pause
return 0; //end program
每当我跑步时,它都表示每次都不正确。我正在检查所有的帽子等等,无法解决问题。
我已经在上面声明了所有其他nessesary fstream文件
先谢谢C:
答案 0 :(得分:0)
这就是你需要的:
bool found = false;
while (getline(userfile, STRINGT) && getline(passfile, STRINGO))
{
if (strcmp(user, STRINGT.c_str()) == 0 && strcmp(pass, STRINGO.c_str()) == 0) //if user and pass are both in file
{
found = true;
break;
}
}
if (!found) {
cout << "Incorrect username or password" << endl;
}