Hello! I'm just a beginner in C Programming. I just want to ask for some help here!
I need to input a number 10, 20 or 0
If I entered a number 100, 2000, or 0000 that will be invalid...
There should only be a limit of two characters.....
and if I entered any no. not equal to 20, 10 or 0
the computer should say "INVALID!"
The computer should continue to ask for a number from the user until he has given a valid input.
Enter a number: 100
Enter choice 10,20,0: 45
Enter choice 10,20,0: 00
Enter choice 10,20,0: Hello
Enter choice 10,20,0: 10
I have to deal with character variables here since if I used integers for my variable... the tendency is... everytime I input characters (ex. "Hello") .... the computer will continuously display Invalid!
I tried using these codes:
char cChoice=" " /*string variable declaration*/
/*ask no. from user*/
printf("Input choice 20, 10, 0 :");
if (strlen(cChoice)>2); /*measures the string length*/
}while(strlen(cChoice)>2); /*repeat statements until answer is valid*/
if (cChoice!='1' && cChoice!='0')
if (cChoice!='2' && cChoice!='0')
if (cChoice!=0 && cChoice!='\0')
I had many problems with these code I created..... The actual output did
not match to my expected output.. and when I'm entering an invalid value the program does not ask anymore for the correct value... Could you help me figure out what is wrong with my codes... I would be glad if you could give me the right codes for this little problem... sorry that I'm asking help for this simple problem.... I'm just a beginner .. you know... Thanks! :cheesy: