因此,我一整天都在嘗試制作這個程式,但我遇到了這個大問題,我無法通過驗證注冊用戶的 txt 檔案中先前存在的資料來登錄帳戶。
注意:userEmail繼承自另一個類并已定義。
這是我的登錄功能:
void logIn() { // ! Function to log into an existing account.
system("cls");
string loginEmail, loginPassword;
bool verifyEmail = false;
cout<<"\n~\t~\tLogging into an existing account..\t~\t~\n";
cout<<"\nEmail: ";
cin.ignore();
getline(cin,loginEmail);
ifstream checkEmail("registeredUsers.txt");
ifstream open("registeredUsers.txt");
while(checkEmail>>userEmail && loginEmail.find('@') != string::npos && loginEmail.length() > 6) {
if (userEmail == loginEmail) {
verifyEmail = true;
}
else {
verifyEmail = false;
}
}
if (verifyEmail == true) {
cout<<"\nPassword: ";
getline(cin,loginPassword);
}
else {
cout<<"Email not found.. Please register a new account.\n";
system("pause");
return;
}
system("pause");
}
假設我的registeredAccounts.txt 有行
[email protected]
[email protected]
[email protected]
然后,如果我輸入我的程式,它只會繼續到密碼部分,如果我[email protected]輸入任何其他電子郵件,它不會讓我繼續。
uj5u.com熱心網友回復:
設定 verifyEmail = true 后立即跳出 while 回圈。如果您已經找到匹配項,則無需檢查其他電子郵件地址。實際上,您的代碼繼續檢查不匹配的電子郵件地址,將 verifyEmail 設定回 false。
例如:
while(checkEmail>>userEmail && loginEmail.find('@') != string::npos && loginEmail.length() > 6) {
if (userEmail == loginEmail) {
verifyEmail = true;
break;
} else {
verifyEmail = false;
}
}
現在,您實際上不需要將 verifyEmail 設定為 false。所以你可以使用:
while(checkEmail>>userEmail && loginEmail.find('@') != string::npos && loginEmail.length() > 6) {
if (userEmail == loginEmail) {
verifyEmail = true;
break;
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/455266.html
標籤:C
上一篇:在c 中增加變數值時的競爭條件
