getline skipping first time

why does this happen that if i use
1
2
3
4
5
6
string ans;
for(int i=1; i<3; i++)
{
cout << "enter an option" << endl;
getline(cin,ans)
}

in a loop it always skips the first time but in the next times it asks?
Last edited on
it doesn't skip, your for() loop goes only two times - when i equals to 1 and 2

rewrite it
for(int i = 1; i <= 3; ++i)
oh yes i got it but actually i mean it.. in another program i am using something same like i ask a question and user is supposed to enter the answer...but it doesn't wait for the first input though in the next iteration it waits...
You probably used cin >> ... (which leaves then end of line in the stream) before getline. Use ignore to remove the end of line:

http://www.cplusplus.com/reference/istream/istream/ignore/
Topic archived. No new replies allowed.