0

Hi all,

This code block is making sure the user is entering the correct character, either y/n.

char response;

cout<<"\nDo you want to continue? (y/n): ";
cin>>response;
cout<<endl;

while(response != 'y' || response != 'n')
{
if (response == 'y' || response == 'n')
break;
cout<<response<<" is an invalid input. Please re-enter your answer (y/n): ";
cin>>response;

}

as you guys can see, an "if...block" doesn't need to be there, but if I remove it, my program will keep asking user to enter valid repsonse character, another words, it's infinity loop. I tested this on VS 1005 express edition and Unix. they are both produced the same result.

if anyone have an idea/something wrong/better way to accomplish this task with this code block, I am greatly appreciated it.

Henry

2
Contributors
4
Replies
5
Views
10 Years
Discussion Span
Last Post by henry.nc
0

Well the thing that doesn't need to be in there is the while loop. Just have it "default" to no so if they enter anything aside from a Y then don't continue.

0

um...it works...however, for entry validation purpose, is something wrong with while..loop?

0

It's an infinite loop because you have || (OR) isntead of && (AND) which pretty much says: while input isn't y or isn't n tell them it's wrong. It should say: while input isn't y AND isn't n tell them it's wrong.

Take out the ifcheck and replace || with &&, there it's fixed :)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.