getline() not reading all lines in txt file
Dec 8, 2012 at 10:45pm UTC
So I made this function to check username and password. I've put two usernames and two password in each file. I tried to use a loop to read all lines but it's not working. I'm only able to log in with just user1 account. pw & username text file is setup like this
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
void auth( string user, string pass )
{
string inpass, inuser;
ifstream passfile;
ifstream userfile;
passfile.open ("password.txt" );
userfile.open ("username.txt" );
if (userfile.is_open() && passfile.is_open() )
{
while (getline(passfile, inpass))
{
while (getline(userfile, inuser))
{
userfile.close();
passfile.close();
do {
if (user==inuser && pass==inpass)
{
system("cls" ); //command line function to clear the screen
cout << "Welcome " << user;
break ;
}
else
{
cin.clear();
cout << "Username or password is incorrect. Please try again." << endl;
cout << "Please enter your username: " ;
cin >> user;
cout << "Please enter your password: " ;
cin >> pass;
}
}while (true );
}
}
}
else cout << "Unable to open file" ;
}
Topic archived. No new replies allowed.