Hey guys,
This may seem familiar to a few of you, I posted the same bit of code a couple days ago, but I have a different problem with it this time. The loop reads lines of a file that contains usernames and passwords. This is the whole function. Also, I know goto statements aren't optimal, but I plan on changing it to eliminate them once I have it running.
Code:
void newName() {
string userName, password;
string existingName;
int i = 0;
cout << "Opening username list info. . ." << endl;
ifstream userNameDB("users.data");
/*if (!userNameDB) {
cout << "Error opening 'users.data'." << endl;
cout << "Writing error report. . ." << endl;
writeError(0);
cout << "Returning to login:" << endl;
logIn();
}*/
usernameCreate:
i = 0;
cout << endl << "Enter your desired username(4-12 characters, LETTERS ONLY): ";
cin >> userName;
if (userName.length() > 12 || userName.length() < 4) {
cout << endl << "Invalid input, check username length." << endl;
goto usernameCreate;
}
for (i = 0; userNameDB >> existingName; i++) {
if (userName == existingName) {
cout << "Name already exists, please pick a different one. " << endl;
goto usernameCreate;
}
}
userNameDB.close();
passwordCreate:
cout << "Enter a password(4-12 characters, LETTERS ONLY): ";
cin >> password;
if (password.length() < 4 || password.length() > 12) {
cout << "Invalid input, check username length." << endl;
goto passwordCreate;
}
cout << "Saving info. . ." << endl;
createName(userName, password);
}
If the file looks like this:
Code:
//users.data
Username
Password
Username2
Password2
Username3
Password3
And I try to create another username "Username", it catches it the first time and tells me that it already exists. But if I try to create a username "Username" right after the first attempt, it's not caught that it already exists. I've been staring and thinking for at least an hour, I can't figure it out. Please help .
Thanks in advance.