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