interesting input issue.

Feb 21, 2013 at 3:17am
Here's my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
int talkto(string name){
    string talktoinput;
    talktobase:
    cout << "\n\nTalk to " << name <<" ? (y/n)";
    getline (cin, talktoinput);
    if (talktoinput=="y"){
        return 1;
    }
    else if (talktoinput=="n"){
        return 0;
    }
    else{
        cout << "\nI don't understand...";
        goto talktobase;
    }
}


when called, this function outputs:
Talk to Maria? (y/n)

I don't understand...
Talk to Maria? (y/n)

And then takes the input and functions perfectly.
Does anybody know why it feels the need to fun through the error message exactly once?

Thanks in advance!
Feb 21, 2013 at 3:59am
not sure why your having that issue. i tested it exactly as is in linux using g++ and it works the first time for both cases.
Feb 21, 2013 at 5:30am
How are you calling the talkto() function?

There are some unread chars on cin which are read the first time round.
I would think some time before calling talkto() you're reading cin, but not reading all of it.

Jim
Topic archived. No new replies allowed.