Here’s the code :
cout << "Please enter the file path: ";
string sPath;
getline(cin, sPath);
cout << "Please enter the password: ";
string sPassword; getline(cin, sPassword);
Problem is, when I run it it displays “Please enter the file path: ” then it displays “Please enter the password: ” and then waits for the password. It seems to completely skip the first ‘getline()’.
Later edit: Yes there are some input operations done before.
int iOption = 0;
while (iOption == 0)
{
cout << "(E/D): ";
switch (GetCH())
{
case 'E':
iOption = 1;
break;
case 'e':
iOption = 1;
break;
case 'D':
iOption = 2;
break;
case 'd':
iOption = 3;
break;
default:
break;
}
}
And the code for GetCH() in case anyone asks.
char GetCH ()
{
char c;
cin >> c;
return c;
};
It looks like the rest of the line that was input for
GetCHstill remains in the buffer at the time that you callgetline, i.e. at least a\nand this is what you are reading in the firstgetlinecall. The program doesn’t block waiting for user input because thegetlinerequest can be satisfied by the partial line still queued for reading.Consider modifying your
GetCHfunction to read whole lines as well.E.g. something like (totally untested, I’m afraid):