0
我想了解更多关于C++的知识,并且已经完成了验证登录的任务。Bool结果从文件中读取数据后返回false
到目前为止,我有
#include <iostream>
#include <istream>
#include <fstream>
#include <string>
using namespace std;
void getLogin(string & userName, int password);
bool validateLogin(string userName, int password);
void showResult(bool validation);
int main()
{
int password = 0;
string userName;
bool validation;
getLogin(userName, password);
validation = validateLogin(userName, password);
showResult(validation);
return 0;
}
void getLogin(string & userName, int password)
{
cout << "Enter your ID: ";
cin >> userName;
cout << "Enter your PW: ";
cin >> password;
}
bool validateLogin(string userName, int password)
{
string user;
int pass;
ifstream inFile;
inFile.open("C:\\login.txt");
if (inFile.fail())
{
cout << "Error finding file";
exit(1);
}
getline(inFile, user);
inFile >> pass;
if (userName == user && password == pass)
{
return true;
}
else
{
return false;
}
}
void showResult(bool validation)
{
if (validation == true)
{
cout << "Valid\n\n";
}
else
{
cout << "Invalid\n\n";
}
}
里面的login.txt文件,用户名和密码已经被写入。提示要求用户输入他们的用户名和密码。当我输入txt文件内的用户名和密码时,它总是显示为无效。 Link to output and login.txt
'int'的密码?这真的很奇怪。另外,如果某件事是'布尔',你可以'如果(验证)'。比较'== true'就好像在说“如果是真的”。 – tadman