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.