Thread: Counting the number of case selections in a menu

  1. #1
    Registered User
    Join Date
    Sep 2013
    Posts
    6

    Counting the number of case selections in a menu

    There's a similar question to this below me, but I thought it would be easier to answer if there was an example to refer to.

    Code:
      #include<stdio.h>
    
    int main () {
        int choice, opens, saves, news;
    
        printf("What would you like to do?\n");
        printf("\t1 - Open\n");
        printf("\t2 - Save\n");
        printf("\t3 - New\n");
        printf("\t4 - Display selections\n");
    
        scanf("%d", &choice);
    
    while (choice != 4){
       switch(choice) {
            case 1:
                printf("Opening..");
                break;
    
            case 2:
                printf("Saving..");
                break;
    
            case 3:
                printf("Creating new..");
                break;
    
            case 4:
                break;           
    
            default:
                printf("Invalid Input.");
                break;
       
     } //switch close
    
    
        printf("What would you like to do?\n");
        printf("\t1 - Open\n");
        printf("\t2 - Save\n");
        printf("\t3 - New\n");
        printf("\t4 - Display selections\n");
        scanf("%d", &choice);
    
    
    } //while close
    
    printf(" %d opens, %d saves, %d new files", opens, saves, news);
    
     return 0;
    }
    So what would I have to add in order for this code to display how many times the user has selected open, save or new? I'd really appreciate the help.

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    You're kidding right? Initialise opens, saves, news to zero. Statements that increment opens, saves, or news in appropriate places would do the trick.

    More generally, I'd use an array of ints, initialised to all zeros. Do bounds checking on choice, and then increment array[choice] for valid inputs. Same technique will work for counting number of times user has been told off for invalid input (just increase array size by 1, and adjust bounds checking). And that will work regardless of number of case statements.

    Instead of using magic values 1,2,3,4 try using an enumerated type.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  3. #3
    Registered User
    Join Date
    Sep 2013
    Posts
    6
    Thank you grumpy! I feel silly now, I can't believe didn't realize how simple it was. I got it working now

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Switch Case for Counting Frequency of numbers
    By sjmp in forum C Programming
    Replies: 11
    Last Post: 07-30-2012, 12:35 PM
  2. Menu Selections in a command prompt
    By Shamino in forum C++ Programming
    Replies: 5
    Last Post: 01-24-2012, 12:45 PM
  3. Counting Numbers in Array, not counting last number!
    By metaljester in forum C++ Programming
    Replies: 11
    Last Post: 10-18-2006, 11:25 AM
  4. Help with menu and integer selections
    By EliasK in forum C Programming
    Replies: 2
    Last Post: 04-03-2003, 09:35 AM
  5. Help me in while loop with case menu
    By playboy1620 in forum C Programming
    Replies: 1
    Last Post: 02-20-2002, 11:07 PM

Tags for this Thread