getline() in if statement

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



Please help to resolve it.
Last edited on
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).
Add this line...
cin.ignore()

Right after the line...
cin >> option;
Thanks very much Splux and iHutch for the reply

Both codes worked effectively.

@Splux I understood what you trying to explain about stream. Now i know why it doesnt read the line.
Thank you
Also, iHutch had the better version... :p

And good that you understood, I'm pretty crappy at explaining stuff.
Splux-your explanation reached my mind and
iHutch -your code reached my program
;)
Thanks both of u
Topic archived. No new replies allowed.