2 cin.getlines()??
Hello,
Why is it that this code needs two
cin.getline(response, 4);
?
If I run it with one it still works, but the output is
Restart program? Invalid, enter (y/n) |
as opposed to simply asking the user if they want to restart the program.
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 30 31 32 33 34 35 36 37 38
|
bool restart() //returns false if the user wants to restart the program
{
char response[4]; //holds the users response
string answer;
stringstream convert;
cout<<"Restart the program? ";
cin.getline(response, 4);
cin.getline(response, 4);
while(true)
{
//convert response to lowercase
for(int i=0; i<strlen(response); i++)
{
response[i]=tolower(response[i]);
}
convert<<response; //write to 'convert'
answer=convert.str();
if(answer=="y" || answer=="yes") //if yes
{
clr(); //clear the screen
return false;
}
else if(answer=="n" || answer=="no") //if no
{
return true;
}
else
{
cout<<"\aInvalid, enter (y/n). ";
cin.getline(response, 4);
}
}
}
|
Try putting cin.ignore(); above the output and remove a getline.
Thanks Grex2595.
Topic archived. No new replies allowed.