I have been messing with this so much that its so cluttered. I need a way to let get line check next line after a semi colon.
1 2 3 4 5
while(!openFile.eof()){
getline(openFile, tempUser, ';');
if(tempUser == username){cout << "Username exsists, please select a new username: " << endl; break;}
};
So what this does is lets say I have USERNAME;PASSWORD it simply checks username then password, instead of going to next line to check only the usernames. I have been stuck on this project for a few days now, and would really appreciate the help. Ty.
Edit: Please keep it simple, Im a beginner and want to master the basics.
std::map<std::string, std::string> name_pass_map;
std::string username;
std::string password;
//...Load data from file
std::string hold;
while(std::getline(openFile, hold)){
name_pass_map.insert(std::make_pair(
hold.substr(0, hold.find(';')),
hold.substr(hold.find(';')+1)
));
hold.clear();
}
//...Get username
//Check if the name already exists in the database
if(name_pass_map.find(username) != name_pass_map.end())
cout << "Error. User already exists. Choose another name.\n";
//...Get password
name_pass_map[username] = password; //Register new user
// Let hold == "Username;Password"
name_pass_map.insert(std::make_pair(
hold.substr(0, hold.find(';')), // Get "Username" substring
hold.substr(hold.find(';')+1) // Get "Password" substring
And partially the reason why I dont want a map to check is so that I can load username and password data via files.
Essentially the map is there to check user/pass if the file cannot open as an error handling.
I apologize, but I don't really understand what you're trying to achieve. Are you saying you want to check the usernames as you're reading the file? Versus loading everything to a database first before checking?
You can do it, but I suggested preloading because I thought you were going to do something else with the map (like inserting the new username and password in the same database).
Let me try to guess what you're doing:
- Use a file as a source of usernames and passwords to check from
- If the file cannot be opened, default to a member map that also contains a list of usernames and passwords?
- Whether the file could be opened or not, always register the new user in the member map?
Make pair is basically the same as doing UserPass[username] = password, but ur chopping it up via strings correct? (im new to strings, etc)
This has been my current fix:
1 2 3 4 5 6 7
while(!openFile.eof()){
getline(openFile, tempUser, ';');
getline(openFile, tempPass);
if((tempUser == username)&&(tempPass == password)){
wrongUser = 1;
cout << "Thank you for logging in. " << endl;
switchLog(account);
I was reading username only before, so it was reading the password instead of going to new line, I was told to include both getline's as such so it goes to the new line.
All in all your fix is closer to what I'm trying to do with error handling (will prob run it at beginning of program to eliminate any future errors with file handling. (To ensure the map is pre-loaded).