help with a foolproof input
Oct 2, 2015 at 6:36am UTC
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".
Oct 2, 2015 at 6:48am UTC
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 Oct 2, 2015 at 6:50am UTC
Oct 2, 2015 at 7:35am UTC
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 Oct 2, 2015 at 7:35am UTC
Oct 2, 2015 at 8:01am UTC
the toupper works :D
Topic archived. No new replies allowed.