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

Recommended Answers

All 4 Replies

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.

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

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 :)

thanks...I see it now.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.