Thread: What is the while equivelent to the default function in the switch statement??

  1. #1
    Registered User
    Join Date
    Nov 2011
    Posts
    14

    What is the while equivelent to the default function in the switch statement??

    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?

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    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

  3. #3
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    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

  4. #4
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    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?

  5. #5
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    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.

  6. #6
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    Quote Originally Posted by Tclausex View Post
    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.

  7. #7
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    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.

  8. #8
    Registered User
    Join Date
    Nov 2011
    Posts
    14
    how would i do this??sorry i'm such a noob.

  9. #9
    Registered User
    Join Date
    Nov 2011
    Location
    Saratoga, California, USA
    Posts
    334
    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.

  10. #10
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    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;
      }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. HELP! How do I execute a function in a switch statement??
    By kkmoslehpour in forum C Programming
    Replies: 34
    Last Post: 10-13-2011, 08:40 PM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  3. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  4. default switch statement not working
    By gL_nEwB in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2006, 10:13 AM
  5. Returning from function - switch selects default
    By rkooij in forum C Programming
    Replies: 24
    Last Post: 03-17-2006, 07:27 AM