Hello I have to implement a simple user name and password checking algorithm. Valid user names and associated passwords are contained in a text file named passwords.txt . The form of this file is as follows:
I need to have a loop that does not end unless both the user name and password are entered correctly. The code below works fine if the user name and password are entered correctly. What is does not do is repeat from the beginning if EITHER the user name or the password are incorrect. That is what I need with a message saying user name not found or password not found. Can anyone PLEASE help me with this! It is for an assignment which is complete except for this, I can't figure it out honest!Code:///passwords.txt admin 1 pass1 admin2 pass2 admin3 pass3
Thanks for your timeCode:#include <iostream> #include <string> #include <fstream> #include <iomanip> using namespace std; int main () { string firstName, lastName, userName, password; cout <<"\n"; cout << setw(30) << left << "\n\tPlease login, login must consist of only TWO names seperated by a space:\n" << right ; cout << "\n\tEnter name:\t"; cin >> firstName >> lastName; userName = firstName+" "+lastName; ifstream in("passwords.txt"); string inbuf; while( !in.eof()) { getline(in,inbuf); if(inbuf == userName) { cout << "\n\tUser: " << userName << " found.\n\n\tPlease enter your password: " ; cin >>password; getline(in,inbuf); if(inbuf == password) { cout << "\n\tPassword verified."<< endl ; } else { cout << "\n\tPassword incorrect. " << endl; } } } in.close(); return 0 ; }
ceevee



LinkBack URL
About LinkBacks



