Question
C++ language [4 Points] Write a program that performs a survey tally on beverages. The program should prompt for the next person until a sentinel
C++ language
- [4 Points] Write a program that performs a survey tally on beverages. The program should prompt for the next person until a sentinel value of 1 is entered to terminate the program. Each person participating in the survey should choose their favorite beverage from the following list:
- Coffee 2. Tea 3. Coke 4. Orange Juice
Sample Run:
Please input the favorite beverage of person #1: Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program
4
Please input the favorite beverage of person #2: Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program
1
Please input the favorite beverage of person #3: Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program
3
Please input the favorite beverage of person #4: Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program
1
Please input the favorite beverage of person #5: Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program
1
Please input the favorite beverage of person #6: Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program
-1
The total number of people surveyed is 5. The results are as follows: Beverage Number of Votes
******************************** Coffee 3
Tea 0
Coke 1
Orange Juice 1
Part II [12 Points + 5 Bonus]
4. (a) Population bar chart: Write a program that asks the user to enter the
population. of 4 cities and produce the bar graph
Here is an example of programs output. User input is shown bold. Please enter number of cities: 4
Enter the population of city 1 : 10000 Enter the population of city 2: 15000 Enter the population of city 3: 9000 Enter the population of city 4: 18000
POPULATION
(each * = 1000 people)
City 1 : **********
City 2 : *************** City 3 : *********
City 4 : ******************
Input validation: Do not accept population less than 0.
Here is an example of programs output if user input is less than zero. (Error message is in Red. (Assume that user enter 3 for number of cities in this case)
Enter the population of city 1 : -20 Enter the population of city 2 : 3000 Enter the population of city 3 : 4000 Enter the population of city 4 : 8000
Population cant be negative. Please re-enter
Enter the population of city 1 : 2000 Enter the population of city 2 : 3000 Enter the population of city 3 : 4000 Enter the population of city 4 : 8000
POPULATION
(each * = 1000 people)
City 1 : ** City 2 : *** City 3 : ****
City 4: ********
Hint: For this program you need to use selection and repetition commands. Consider using a do-while loop. Also, there are many ways of writing this program. I will accept any valid implementation that users proper C++ constructs
(b) [5 Points Bonus] Modify your program above that asks the user to enter number of cities he/she would like to enter population. Then ask the user to enter the population for each city and produce the bar graph
Here is an example of programs output. User input is shown bold. Please enter number of cities: 4
Enter the population of city 1 : 10000 Enter the population of city 2: 15000 Enter the population of city 3: 9000 Enter the population of city 4: 18000
POPULATION
(each * = 1000 people)
City 1 : **********
City 2 : *************** City 3 : *********
City 4 : ******************
Input validation: Do not accept population less than 0.
Here is an example of programs output if user input is less than zero. (Error message is in Red. (Assume that user enter 3 for number of cities in this case)
Enter the population of city 1 : -20 Enter the population of city 2 : 3000 Enter the population of city 3 : 4000
Population cant be negative. Please re-enter
Enter the population of city 1 : 2000 Enter the population of city 2 : 3000 Enter the population of city 3 : 4000
POPULATION
(each * = 1000 people)
City 1 : ** City 2 : *** City 3 : ****
Hint: For the bonus part, you will need to use dynamic memory allocation and arrays. Example program below.
//Example of Dynamic memory allocation
#include
using namespace std;
int main ()
{
int i,n;
int * p;
cout << "How many numbers would you like to type? ";
cin >> i;
p= new int[i]; // here memory is allocated according to the user input.
for (n=0; n
{
cout << "Enter number: ";
cin >> p[n];
}
cout << "You have entered: ";
for (n=0; n
cout << p[n] << ", ";
delete[] p; //deallocating memory.
return 0;
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started