need help for string condition whit a while

the code is in french but that's not the problem ,

string PseudoOK;
cout << "votre pseudonyme est: " << pseudo << ", confirmer? Y/N"<< endl;
getline(cin, PseudoOK);
{
if(PseudoOK == "Y" )
{
cout << "bon jeu !" << endl;
}
else if(PseudoOK == "N")
{
cout << "relancer le jeu pour recommencer" << endl;
}
else
{
cout << "reponse invalide , merci de relancer le jeu :)" << endl;
}
return 0;
}
and i whant a while somwhere so the program keep asking Y or N untill the user write Y or N.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
string PseudoOK;
bool answerOK(false);

while (cin && ! answerOK)
{
    cout << "votre pseudonyme est: " << pseudo << ", confirmer? Y/N"<< endl;
    getline(cin, PseudoOK);
    if (PseudoOK == "Y" )
    {
        cout << "bon jeu !" << endl;
        answerOK = true;
    }
    else if (PseudoOK == "N")
    {
        cout << "relancer le jeu pour recommencer" << endl;
        answerOK = true;
    }
    else
    {
        cout << "reponse invalide , merci de relancer le jeu :)" << endl;
    }
}

if (! cin && ! answerOK)
{
    cerr << "input error" << endl;
}

return 0;



(And please indent your code and surround it with this nice code-tags. Use "<>"-button at right of the message composing window.)
thank you :) it worked
if (! cin && ! answerOK)
{
cerr << "input error" << endl;

can you please explain to me what does that mean ?
cin may represent not only a keyboard but also a f.e. a disk file or an input from a scanner, ... This devices - even a keyboard - may produce errors. A keyboard may be unplugged from your computer while typing, a disk may be corrupt, ... This final conditional statement does handle this fact by reporting an error message to cerr. cerr is the standard error channel, which usually is your display but may also be a log file or a printer, ...

(See http://www.cplusplus.com/reference/iolibrary/ for cin)



EDIT: Added final io-library reference.
Last edited on
Topic archived. No new replies allowed.