0

What have I've done wrong? I'm entering correct credentials and program keep saying "Invalid username or password!"

void user::login() { string username, uname; string password, pword; cout << "Enter your username: \n"; cin >> uname; cout << "Enter your password: \n"; cin >> pword; ifstream data("data.txt"); if (data.is_open()) { while (!data.eof()) { data >> username >> password; if (uname == username && pword == password) { cout << "Login successfully!\n"; Sleep(2000); mainMenu(); } else if (uname != username || pword != password) { cout << "Invalid username or password!\n"; Sleep(2000); login(); } } data.close(); } } 
2
  • 2
    Why dont you try printing username and password and see whats wrong? Besides that calling 'eof' in a loop is wrong, stackoverflow.com/questions/5605125/… Commented Dec 8, 2015 at 12:36
  • 2
    You recurse into the function, which then opens the file again from the beginning. You should not be calling login inside of login, but continuing on with the while loop (checking for eof is not good though...) to keep reading the file for a match. Commented Dec 8, 2015 at 12:36

1 Answer 1

2

Please see my inline comments:

#include <string> #include <iostream> #include <fstream> // You should not use using namespace std! In here only for brevity. using namespace std; // Use a proper API: return account name on success, empty string otherwise. // Throw an exception on error. string login() throw (const char*) { // embrace C++11! if (ifstream data {"data.txt"}) { string username, uname; string password, pword; // Always test if the IO is broken! if (!((cout << "Enter your username: \n") && (cin >> uname) && (cout << "Enter your password: \n") && (cin >> pword))) { throw "Could not get user credentials."; } // debug output, remove in production: cerr << "Credentials: " << uname << " (" << pword << ")" << endl; // read a line from data.txt while (data >> username >> password) { // debug output, remove in production: cerr << "Read: " << username << " (" << password << ")" << endl; // compare input: if ((uname == username) && (pword == password)) { return username; } } // no input matched return ""; // no need to explicitly close the stream } else { throw "Password file could not be opened."; } } int main() { string account_name; try { // The caller should include a loop, not the login function. account_name = login(); } catch (const char *message) { cerr << "Error: " << message << endl; return 1; } if (!account_name.empty()) { cout << "Hello, " << account_name << endl; } else { cout << "Could not authenticate!" << endl; } return 0; } 

data.txt:

Neuroosi hunter2 Kay supersecret Arunmu hello crashmstr password1234 
Sign up to request clarification or add additional context in comments.

1 Comment

@Neuroosi, you're welcome. Please consider do click the check mark on the left of my answer to mark the question to as solved.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.