Thread: HW Help - Switch and Loop statements

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

    HW Help - Switch and Loop statements

    I'm having difficulty writing this program for my homework assignment, can anybody help me fix this program.

    "This program requires you to use a loop as well as the switch statement. You will display this menu on the screen.


    (Press 9 to stop voting)
    The Many Colors of M&M's Candy
    1. red
    2. blue
    3. brown
    4. yellow
    5. green
    6. purple
    Vote on your favorite color by pressing the number before it.


    In this program, you will keep track of the number of votes for each color of M&M's candy. The user will be able to vote until he/she types the sentinel character 9. Display the menu and ask the user to enter a number. Check the number entered against the numbers 1 - 2 - 3 - 4 - 5 - 6. If the number entered is 1, add one to the redctr. If the user has entered 2, add one to the bluectr, etc.


    When the user has pressed the 9 to exit, print out a display telling how many votes each color received."


    Code:
    #include<stdio.h>
    
    int main()
    {
     int num, redctr, bluectr, brownctr, yellowctr, greenctr, purplectr;
    
        printf("You will display this menu on the screen.
    (Press 9 to stop voting)
    The Many Colors of M&M's Candy
    1. red
    2. blue
    3. brown
    4. yellow
    5. green
    6. purple
    Vote on your favorite color by pressing the number before it.");
            scanf("%2d",&num);
    
     while ( num < 10 ) {
    
    switch ( num ) {
    case 1:
      redctr++;
        printf("You will display this menu on the screen.
    (Press 9 to stop voting)
    The Many Colors of M&M's Candy
    1. red
    2. blue
    3. brown
    4. yellow
    5. green
    6. purple
    Vote on your favorite color by pressing the number before it.");
            scanf("%2d",&num);
      break;
    
    case 2:
      bluectr++;
        printf("You will display this menu on the screen.
    (Press 9 to stop voting)
    The Many Colors of M&M's Candy
    1. red
    2. blue
    3. brown
    4. yellow
    5. green
    6. purple
    Vote on your favorite color by pressing the number before it.");
            scanf("%2d",&num);
      break;
    
    case 3:
      brownctr++;
        printf("You will display this menu on the screen.
    (Press 9 to stop voting)
    The Many Colors of M&M's Candy
    1. red
    2. blue
    3. brown
    4. yellow
    5. green
    6. purple
    Vote on your favorite color by pressing the number before it.");
            scanf("%2d",&num);
      break;
    
    case 4:
      yellowctr++;
        printf("You will display this menu on the screen.
    (Press 9 to stop voting)
    The Many Colors of M&M's Candy
    1. red
    2. blue
    3. brown
    4. yellow
    5. green
    6. purple
    Vote on your favorite color by pressing the number before it.");
            scanf("%2d",&num);
      break;
    
    case 5:
      greenctr++;
        printf("You will display this menu on the screen.
    (Press 9 to stop voting)
    The Many Colors of M&M's Candy
    1. red
    2. blue
    3. brown
    4. yellow
    5. green
    6. purple
    Vote on your favorite color by pressing the number before it.");
            scanf("%2d",&num);
      break;
    
    case 6:
      purplectr++;
        printf("You will display this menu on the screen.
    (Press 9 to stop voting)
    The Many Colors of M&M's Candy
    1. red
    2. blue
    3. brown
    4. yellow
    5. green
    6. purple
    Vote on your favorite color by pressing the number before it.");
            scanf("%2d",&num);
      break;
    
    case 9:
     printf("\Red Total: %d", redctr);
     printf("\Blue Total: %d", bluectr);
     printf("\Brown Total: %d", brownctr);
     printf("\Yellow Total: %d", yellowctr);
     printf("\Green Total: %d", greenctr);
     printf("\Purple Total: %d", purplectr);
     return 0 ;
      break;
    
    default:
     printf("\nInvalid Entry);
     return 0 ;
      break;
    }
    }
    }
    I know that the repeating prompts can be consolidated, but can somebody help guide me.

  2. #2
    SAMARAS std10093's Avatar
    Join Date
    Jan 2011
    Location
    Nice, France
    Posts
    2,694
    I don't see why you use a while loop in line 19.

    Here is what you should do:

    Print the menu, read the input, have a switch statement to decide what counter should you increment.
    Print the menu, read the input, have a switch statement to decide what counter should you increment.
    Print the menu, read the input, have a switch statement to decide what counter should you increment.
    .....
    Print the menu, read the input, have a switch statement to decide what counter should you increment.

    As you see this is a loop. Break the loop when the user types 9.

    Code:
    scanf("%d", &num);
    while(num != 9)
    {
        print Menu;
        scanf("%d", &num);
        switch(num)
        {
            ...
         }
    }
    print the counters
    return 0; /* End of main*/
    }
    Initialize all your counters to zero before using them!!!!

    PS - Welcome to the forum
    Code - functions and small libraries I use


    It’s 2014 and I still use printf() for debugging.


    "Programs must be written for people to read, and only incidentally for machines to execute. " —Harold Abelson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch statements
    By shawnk01 in forum C Programming
    Replies: 3
    Last Post: 07-12-2011, 10:42 PM
  2. switch statements.
    By elixon in forum C Programming
    Replies: 2
    Last Post: 11-30-2006, 05:57 PM
  3. Need help with if /else statements - maybe switch
    By dwinslett in forum C++ Programming
    Replies: 8
    Last Post: 09-23-2003, 07:23 AM
  4. Switch statements??
    By aspand in forum C Programming
    Replies: 9
    Last Post: 05-31-2002, 06:50 AM
  5. Switch statements
    By Unregistered in forum C++ Programming
    Replies: 1
    Last Post: 10-11-2001, 11:28 AM