我的程序正在尝试创建一个新帐户并将用户名与我的数据库中的用户名进行比较。1个来自用户输入,另一个来自向量。我必须遍历向量并比较两个字符串。但由于某些未知原因,它只比较 FIRST 值,而不是比较向量内的所有值。
这是我的代码:
我的 LoginAcc.cpp
class AccountInfo {
public:
string username;
string password;
string type;
};
bool LoginAcc::checkAccountNameValid(string username) {
vector <AccountInfo> accInfo;
AccountInfo user;
ifstream UserDatabase("UserDatabase.txt");
string line = "";
while (getline(UserDatabase,line)) {
stringstream linestream(line);
getline(linestream,user.username,':');
accInfo.push_back(user);
}
UserDatabase.close();
for(vector<AccountInfo>::iterator itr = accInfo.begin(); itr != accInfo.end(); ++itr) {
if (username.compare((*itr).username) != 0)
return true;
else
return false;
}
我的 main.cpp
case 'n':
while (!flag) {
cout << "Please enter a username with 8 characters. " << endl;
cout << "Username : ";
cin >> username;
if (username.length() != 8) {
cout << "Username does not meet the requirements" << endl;
cout << "Username : ";
cin >> username;
}
else {
valid = login.checkAccountNameValid(username);
if (valid == true) {
cout << "Please enter a password with 8 characters." << endl;
cout << "Password : " << endl;
cin >> password;
cout << "1. Student" << endl;
cout << "2. Lecturer" << endl;
cout << "3. Admin" << endl;
cout << "Please choose the category you belong to : ";
cin >> category;
login.createNewAcc(username,password,category);
}
else {
cout << "Username has been taken. Please choose another. " << endl;
cout << "Username : ";
cin >> username;
}
}
}
break;
我觉得我的“checkAccountNameValid”方法中的逻辑有问题。任何人都可以提出建议吗?谢谢!