I want to make sure a user can't enter any characters except 0,1,2 or 3 using a while loop. how do i do it?
I want to make sure a user can't enter any characters except 0,1,2 or 3 using a while loop. how do i do it?
You mean keep on looping until a character other than 0,1,2,3 is entered? I'd use a do-while loop... roughly:
Code:do { Get input from user. If input is not what you want then "break" from loop. } while(1);
"Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
-Christopher Hitchens
You can't prevent them without using some special library, but you can do something like this:
Code:int x = -1; while (x < 0 || x > 3) { printf("Enter a number between 0 and 3: "); scanf("%d", &x); }
C programming resources:
GNU C Function and Macro Index -- glibc reference manual
The C Book -- nice online learner guide
Current ISO draft standard
CCAN -- new CPAN like open source library repository
3 (different) GNU debugger tutorials: #1 -- #2 -- #3
cpwiki -- our wiki on sourceforge
sorry i meant to say that if a person enters a character other than 0,1,2,3 that an error will display. is there any way at all to do this with a while or do while statement?
You make the while test for true if any value other than 0,1,2, or 3 are entered and within the while loop you output the error and reprompt.
How exactly would i do that??? here's the code:
Code:while(i!=5) { printf("2011 Presidential Election \n"); system("pause"); printf("\nPlease Enter a vote for cand1 \n"); printf("Enter 1 for first choice \n"); printf("Enter 2 for 2nd choice \n"); printf("Enter 3 for 3rd choice \n"); printf("Enter 0 for no vote \n"); scanf("%d",&mg[j]); printf("Please Enter a vote for cand2\n"); printf("Enter 1 for first choice \n"); printf("Enter 2 for 2nd choice \n"); printf("Enter 3 for 3rd choice \n"); printf("Enter 0 for no vote \n"); scanf("%d",&da[j]); printf("Please Enter a vote for cand3\n"); printf("Enter 1 for first choice \n"); printf("Enter 2 for 2nd choice \n"); printf("Enter 3 for 3rd choice \n"); printf("Enter 0 for no vote \n"); scanf("%d",&no[j]); printf("Please Enter a vote for cand4 \n"); printf("Enter 1 for first choice \n"); printf("Enter 2 for 2nd choice \n"); printf("Enter 3 for 3rd choice \n"); printf("Enter 0 for no vote \n"); scanf("%d",&gal[j]); printf("Please Enter a vote for cand5 \n"); printf("Enter 1 for first choice \n"); printf("Enter 2 for 2nd choice \n"); printf("Enter 3 for 3rd choice \n"); printf("Enter 0 for no vote \n"); scanf("%d",&hig[j]); printf("Please Enter a vote for cand 6 \n"); printf("Enter 1 for first choice \n"); printf("Enter 2 for 2nd choice \n"); printf("Enter 3 for 3rd choice \n"); printf("Enter 0 for no vote \n"); scanf("%d",&dav[j]); printf("Please Enter a vote for cand7 \n"); printf("Enter 1 for first choice \n"); printf("Enter 2 for 2nd choice \n"); printf("Enter 3 for 3rd choice \n"); printf("Enter 0 for no vote \n"); scanf("%d",&mit[j]); printf("\n Thank you for voting!! \n"); system("pause"); (i++); j++; system("cls"); }
Last edited by Thinlizzy76; 11-14-2011 at 03:34 PM.
MK27 handed you the code for test.
Looking at the code you posted, you'll want to create a function to read in the user's answer.
how would i do this??sorry i'm such a noob.
Well, if you don't know functions yet, leave it for another day. Just repeat the while loop code for each vote, changing the variable you're testing to the appropriate one for that vote.
Easiest approach for you would be to insert the while loop after the scanf(). If the user's data is bad, you'll go into the loop, otherwise it will skip it and go to next vote. Inside the loop, print your error, print a re-prompt for the user, and scanf() again. The loop will continue until the user inputs good data. Then you move on to the next vote.
Here ya go... a voting machine in less than 60 lines...
I know it's not exactly what you want (wouldn't post it if it was)...
but it demonstrates the principles you're interested in...
Code:// the voting machine example // to quit enter a q at the "Press Enter to vote" prompt #include <stdio.h> #include <conio.h> // not standard C99 #define CANDS 5 // number of candidates int main (void) { int votes[CANDS] = {0}; char candidate[CANDS][32]; int i; char vote, quit = 0; // get candidate names for (i = 0; i < CANDS; i++) do printf("Enter the last name of candiate #%d : ", i + 1); while(scanf(" %s",candidate[i]) < 1); // let the voting begin while(quit != 'q') { clrscr(); // not standard C99 printf("Your candidates are ...\n"); for (i = 0; i < CANDS; i++) printf("%d) %s\n",i + 1, candidate[i]); do { printf("Your choice (1 to %d) :", CANDS); scanf(" %c",&vote); vote -= '1'; } while((vote < 0) || (vote >= CANDS)); // register the vote votes[vote]++; // prompt next voter clrscr(); // not standard C99 printf("Next voter, press enter to vote :"); while(getchar() != '\n'); quit = getchar(); } // display results clrscr(); // not standard C99 printf("The voting results are...\n"); for (i = 0; i < CANDS; i++) printf("%s\t\t%d\n",candidate[i],votes[i]); printf("\n\n Have a nice day!\n"); return 0; }