no compiler errors, but no place for input
Sep 13, 2013 at 10:34pm UTC
I'm trying to build a program that functions as a beverage survey, and while it compiles with no errors--it automatically adds up the tally without asking for votes, which results in everything adding up to 0.
Instructions:
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:
1. Coffee 2. Tea 3. Coke 4. Orange Juice
Here's the code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int choice, coffee = 0, tea = 0, coke = 0, orangeJuice= 0, personNumber = 1;
cout << "Beverage Survey\n" << endl;
cout << "Which of the following beverages would you consider to be your favorite?\n" << endl;
cout << "1. Coffee " << endl;
cout << "2. Tea " << endl;
cout << "3. Coke " << endl;
cout << "4. Orange Juice " << endl;
cout << endl;
while (choice > 0)
{
cout << "Input favorite beverage of person # " ;
cin >> personNumber;
cout << " Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" << endl;
cout << endl;
personNumber++;
switch (choice)
{
case 1:
coffee++;
break ;
case 2:
tea++;
break ;
case 3:
coke++;
break ;
case 4:
orangeJuice++;
break ;
default : cout << "Please choose one of our available menu items" ;
cin >> choice;
}
}
cout << endl;
cout << "The results are as follows:\n" << endl;
cout << "Beverage of choice:" << setw(40) << "Number of votes:" << endl;
cout << "-----------------------------------------------------------------------" << endl;
cout << "Coffee:" << setw(32) << coffee << endl;
cout << "Tea:" << setw(35) << tea << endl;
cout << "Coke:" << setw(34) << coke << endl;
cout << "Orange Juice:" << setw(26) << orangeJuice << endl;
return 0;
}
Sep 13, 2013 at 10:52pm UTC
Look at line 9, then line 19, see if you can figure out the problem.
Sep 21, 2013 at 5:07am UTC
Fixed it, thanks!
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int choice = 0;
int coffee = 0,tea = 0,coke = 0,orangeJuice = 0;
int total = 0;
int personNumber = 0 ;
do
{
cout << " Enter how many person for survey :" ;
cin >> personNumber;
cout << "Beverage Survey\n" << endl;
cout << "Which beverage of the following do you consider your favorite?\n" << endl;
cout << "1. Coffee " << endl;
cout << "2. Tea " << endl;
cout << "3. Coke " << endl;
cout << "4. Orange Juice " << endl;
cout << endl;
cout << " Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" << endl;
cout << "Input favorite beverage of person #" << personNumber << endl;
cin >> choice;
switch (choice)
{
case 1:
coffee++;
break ;
case 2:
tea++;
break ;
case 3:
coke++;
break ;
case 4:
orangeJuice++;
break ;
}
total++;
personNumber--;
}
while (personNumber > 0 && choice != -1 && total > 0);
cout << "The results are as follows:\n" << endl;
cout << "Beverage of choice:" << setw(40) << "Number of votes: " << total << endl;
cout << "-----------------------------------------------------------------------" << endl;
cout << "Coffee:" << setw(32) << coffee << endl;
cout << "Tea:" << setw(35) << tea << endl;
cout << "Coke:" << setw(34) << coke << endl;
cout << "Orange Juice:" << setw(26) << orangeJuice << endl;
system("pause" );
return 0;
}
Topic archived. No new replies allowed.