Here is my code. The second time that i use getline, it doesn't work. The variable is not given the value that the user inputs. But the first time i use it, it's ok.
#include <iostream>
#include <string>
usingnamespace std;
void main()
{
// define variables to store username and passwrod
string username;
string pass;
// ask user to input username
cout << "Please enter your username : ";
getline (cin, username);
// ask user to input password
/** I think the problem is here.
the pass variable is not given the value input
by the user **/
cout << "Please enter a password : " ;
getline (cin, pass);
// lfc is a valid password to stevie g
if ((pass == "lfc") && (username == "stevie g"))
{
cout << "Access Granted" << endl;
cout << "Welcome MR : " << username << endl;
cout << "Password entered was : " << pass << endl;
}
else
{
cout << "Password/username combination incorrect" << endl;
cout << "You entered the following log in :" << endl;
cout << endl;
/** The username is ok; it appears as the user has input **/
cout << "Username : " << username << endl;
/** the pass variable seems to contain no value
when seen in the console window **/
cout << "Password : " << pass << endl << endl;
}
return;
}
is it seeming to skip to the end of the program then? If so then the buffer for cingetline needs to be cleared before it is used again. this can be done with cin.ignore();
when i use cin >> pass for example, the user enters the password at the console and on pressing enter the value that has been typed in gets assigned to pass.
Now if i use getline (cin, pass) the user has to press 'enter' on the keyboard twice after having entered a password at the console.
How can i get the input of the line with the user pressing enter only once??