Hi, I want to read a line entered by user and store it in a string variable, if the user entered the option '1'.
But the getline(cin,variable) is not working inside a if statement.
here is my code
1 2 3 4 5 6 7 8 9
string title;
int option;
cout<<"Enter an opiton: ";
cin>>option;
if(option==1)
{
cout<<"Enter the title of the book: ";
getline(cin,title);
}
OUTPUT:
after user enters 1, it displays Please enter the title: Press any key to continue
cin.ignore(80, '\n');
Use this before getline and it'll be fine.
cin only reads to first newline, so the newline will still be left behind in the "stream" (can't tihnk of a better word right now, hopefully you understand).