我想創建一個文本文件,以便第一次運行密碼並使用代碼檢查整個字符串以查找以前輸入的密碼。 即使沒有輸入完整的密碼,當前的代碼也會返回前幾個字母的真值。使用特定的字符串創建一個文本文件並使用它來進行比較
int Manager::ManagerView1()
{
Passwordsearch:
system("cls");
string search;
int offset,ErrorVar;
string line;
ifstream Myfile;
Myfile.open("Password.txt");
cout << "Enter your Password :\n";
cin >> search;
if (Myfile.is_open())
{
while (!Myfile.eof())
{
getline(Myfile,line);
if ((offset = line.find(search, 0)) != string::npos)
{
cout << "Password Accepted ..\n";
system("pause");
}
else
{
cout << "Password Incorrect. \nPress\n 1.) Go back to the main screen\n 2.) Re-Enter Password \n";
cin >> ErrorVar;
if (ErrorVar == 1)
{
system("PAUSE");
return 1;
}
else if (ErrorVar == 2)
{
system("PAUSE");
system("cls");
goto Passwordsearch;
}
else
{
cout << "Wrong option !! Please try again\n";
system("PAUSE");
return 1;
}
}
}
}
}
請仔細閱讀[爲什麼的iostream :: EOF裏面算錯了一個循環條件?(http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-考慮-錯誤的)。 –