This program requires you to use a loop as well as the switch statement. You wil
ID: 3864456 • 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
As there is no specific language mentioned , providing solution in C++
#include <iostream>
using namespace std;
int main() {
int redCtr = 0, blueCtr = 0, brownCtr = 0, yellowCtr= 0, greenCtr = 0, purpleCtr = 0;
int choice;
int vote;
cout<<"Please vote for your M & M Candy for the below provided colors "<<endl;
cout<<"1 for Red"<<endl;
cout<<"2 for Blue"<<endl;
cout<<"3 for Brown"<<endl;
cout<<"4 for Yellow"<<endl;
cout<<"5 for Green"<<endl;
cout<<"6 for Purple"<<endl;
cout<<"9 to exit"<<endl;
cout<<"Want to enter vote : 1 for yes"<<endl;
cin>>vote;
if(vote == 1)
{
cout<<"Please enter your choice";
cin>>choice;
while(vote == 1)
{
switch(choice)
{ case 1:
{redCtr++;
cout<<"Want to enter more vote 1 for yes or 0 for no and please enter 9 to exit when asked to select option for vote"<<endl;
cin>>vote;
cout<<"Please select your option to vote"<<endl;
cin>>choice;}
case 2 :
{blueCtr++;
cout<<"Want to enter more vote 1 for yes or 0 for no and please enter 9 to exit when asked to select option for vote"<<endl;
cin>>vote;
cout<<"Please select your option to vote"<<endl;
cin>>choice;}
case 3:
{ brownCtr;
cout<<"Want to enter more vote 1 for yes or 0 for no and please enter 9 to exit when asked to select option for vote"<<endl;
cin>>vote;
cout<<"Please select your option to vote"<<endl;
cin>>choice;}
case 4:
{ yellowCtr++;
cout<<"Want to enter more vote 1 for yes or 0 for no and please enter 9 to exit when asked to select option for vote"<<endl;
cin>>vote;
cout<<"Please select your option to vote"<<endl;
cin>>choice;}
case 5:
{greenCtr++;
cout<<"Want to enter more vote 1 for yes or 0 for no and please enter 9 to exit when asked to select option for vote"<<endl;
cin>>vote;
cout<<"Please select your option to vote"<<endl;
cin>>choice;}
case 6:
{ purpleCtr;
cout<<"Want to enter more vote 1 for yes or 0 for no and please enter 9 to exit when asked to select option for vote"<<endl;
cin>>choice;
cout<<"Please select your option to vote"<<endl;
cin>>choice;
}
case 9:
{ cout<<"You have choosen to exit";
break;}
default:
{ cout<<"Invalid option";
cout<<"Want to enter more vote 1 for yes or 0 for no and please enter 9 to exit when asked to select option for vote"<<endl;
cin>>choice;
cout<<"Please select your option to vote"<<endl;
cin>>choice;
}
}
}
cout<<"The count of Red vote is " <<redCtr<<endl;
cout<<"The count of Brown vote is " <<blueCtr<<endl;
cout<<"The count of Brown vote is " <<brownCtr<<endl;
cout<<"The count of Yellow vote is " <<yellowCtr<<endl;
cout<<"The count of Green vote is " <<greenCtr<<endl;
cout<<"The count of Purple vote is " <<purpleCtr<<endl;
}
else
{
cout<<"You haven't voted at all";
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.