Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This program requires you to use a loop as well as the switch statement. You wil

ID: 3768996 • Letter: T

Question

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

red

blue

brown

yellow

green

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.

Explanation / Answer

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
int redcnt=0,bluecnt=0,greencnt=0,yellowcnt=0,purplecnt=0,browncnt=0,n;
  
   do{
   printf("(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("%d",&n);
   switch(n){
   case 1: redcnt++;
   break;
   case 2: bluecnt++;
   break;
   case 3: browncnt++;
   break;
   case 4: yellowcnt++;
   break;
   case 5: greencnt++;
   break;
   case 6: purplecnt++;
   break;
   case 9: printf(" 1.red = %d 2.blue=%d 3.brown=%d 4.yellow=%d 5.green=%d 6.purple=%d ", redcnt,bluecnt,browncnt,yellowcnt,greencnt,purplecnt);
   scanf("%d",&n);
   return 0;
  
   }
   }while(1);
   return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote