help with a foolproof input

hello
as the title says i need help making a foolproof input
for example the output should be like this.

Output:

enter y or n:
sadsadsaas
invalid input try again

so far i tried using loop
here's my code:

1
2
3
4
while ((choice != 'y')&&(choice != 'Y')&&(choice !='n')&&(choice !='N'))
{
		cout<<"\nInvalid";
}


it's annoying to use because i have to make 4 choices so it'll make the code longer,gets confusing etc etc. and if i enter "sadasdasdasdsadas" it'll output multiple "Invalid".
closed account (48T7M4Gy)
Take advantage of uppercase functions:

1
2
3
4
5
6
char choice = blah blah

while ( toupper(choice) != 'Y' and toupper(choice) != blah blah )
{
		cout<<"\nInvalid";//typo
}
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
void yesno;
char yn;
if(yn == 'y' || yn == 'Y')
{
     //your code
}
else if(yn == 'n' || yn == 'N')
{
    //your code
}
else
{
    cout<<"Invalid Input.. Try Again";
    yesno;
}


Try out if this works....
Last edited on
the toupper works :D
Topic archived. No new replies allowed.