當我運行我的程序時,用戶可以登錄,但是如果輸入錯誤的用戶名,它會再次運行一個檢查用戶名循環,表示他們沒有輸入有效的用戶名。除了一件事外,這項工作非常好。假設他們嘗試三次登錄,第三次嘗試是正確的,並且提示輸入密碼。一旦他們輸入它,它要求第二個密碼,然後第三個。它似乎正在完成其他嘗試的功能。我想不出一個辦法來檢查這一點。有任何想法嗎。C++函數運行超過預期
如果你看看它,你可以看到我打電話UserCheck
在getNameIndex
裏面。我幾乎是積極的,這是錯誤發生的地方。
,檢查用戶的功能:
void User_Psw::UserCheck()
{
// read from the database
ifstream fin("dataBase.txt", ios::in);
if(!fin.good())
{
cout << "Failed to open database file." << endl;
return;
}
while (fin >> username >> password)
{
Usernames.push_back(username);
Password.push_back(password);
++sizeOfDatabase; // This may or may not be needed elsewhere.
}
// rest of the program
cout << "Username: ";
cin >> username;
getNameIndex();
cout << "Password: ";
cin >> password;
if(!PasswordMatches())
{
cout << "Access denied";
}
else
{
cout << "Success! You have logged in.";
}
}
這是用戶名檢查功能
void User_Psw::getNameIndex()
{
userThere = false;
for(int i=0; i < sizeOfDatabase; i++)
{
if (Usernames[i] == username)
{
index = i;
userThere = true;
}
}
if (userThere == false)
{
cout << "\nThat user name does not exsist. \n";
cout << "Please try again. \n\n";
UserCheck();
}
}
你對問題的根源是絕對正確的,但這不是一個真正的錯誤,程序完全按照你所說的去做。你需要重新思考你的功能設計。 'UserCheck()'應該處理所有的輸入/輸出,'getNameIndex()'不應該做任何這樣的事情,只驗證輸入並返回db中用戶的'bool'或索引。 – us2012 2013-02-10 04:59:22