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.

SAMPLE RUN
Enter a number: 100
Invalid!

Enter choice 10,20,0: 45
Invalid!

Enter choice 10,20,0: 00
Invalid!

Enter choice 10,20,0: Hello
Invalid!

Enter choice 10,20,0: 10
Valid!

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:
#include <stdio.h>
#include <string.h>
main()
{
char cChoice[]=" " /*string variable declaration*/
do
{
/*ask no. from user*/

printf("Input choice 20, 10, 0 :");
scanf("%s", &cChoice);
if (strlen(cChoice)>2); /*measures the string length*/
printf("Invalid!");
}while(strlen(cChoice)>2); /*repeat statements until answer is valid*/
if (cChoice[0]!='1' && cChoice[1]!='0')
printf("Invalid!');
nResult=0;
break;
if (cChoice[0]!='2' && cChoice[1]!='0')
printf("Invalid!');
nResult=0;
break;
if (cChoice[0]!=0 && cChoice[1]!='\0')
printf("Invalid!');
nResult=0;
break;
}while(nResult==0);
printf("Valid!');
}

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:

Greetings.
I'm no C expert but I hope I can help you.
Please check your do..while loop as I see 2 "whiles" there with only 1 "do".
Erm, honestly, I think you should look into methods like isAlpha(int) and isDigit(int). Things would be easily done using these methods. Try it first and see what you can do with them. ;)

Also, check out this tutorial here:-
Character Handling
Hope it helps.

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.