survery program not printing results
Sep 21, 2013 at 4:09am UTC
I'm trying to build a program that functions as a beverage survey, and while it compiles with no errors--it doesn't print out the results of the survey, so I'm not even sure if it's working correctly other than that.
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
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 72 73 74 75 76 77 78 79
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int choice = 0;// choose beverages
int coffee = 0,tea = 0,coke = 0,orangeJuice = 0; // four beverages
//Initializing all variables
int total = 0; // total
int personNumber = 0 ; //
cout << " Enter how many person for survey :" ;
cin >> personNumber;
{
int coffee = 0, tea = 0, coke = 0, orangeJuice= 0;
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;
cout << endl;
return 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" ;
}
}
if (choice != -1)
{
cout << endl;
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;
return 0;
}
}
Last edited on Sep 21, 2013 at 4:10am UTC
Sep 21, 2013 at 5:07am UTC
Nevermind, I fixed it. For anybody else who needs it, here's the corrected 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 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;
}
Sep 21, 2013 at 5:16am UTC
The first problem is you initialize again your variables at line 23:
int coffee = 0, tea = 0, coke = 0, orangeJuice= 0;
you should delete this line, it's useless unless you will do a loop and if so...
you should remove int since it is already declared then, replace the commas with semicolon.
starting at line 36, l assume you want to do a loop, until all person has given their vote; you can use a for loop for that.
at line 40, i don't know what that does mean
however this is my version:
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 72 73 74 75 76 77 78 79
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int choice = 0;// choose beverages
int coffee = 0,
tea = 0,
coke = 0,
orangeJuice = 0; // four beverages
//Initializing all variables
int total = 0; // total
int personNumber = 0 ; //
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;
for ( int i = 1; i <= personNumber; i++ ) {
cout << "Input favorite beverage of person # " << i << endl;
cin >> choice;
if ( choice == -1 )
return 0;
cout << endl;
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" ;
}
}
cout << endl;
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;
return 0;
}
tab characters from notepad++ messes my post
Last edited on Sep 21, 2013 at 5:24am UTC
Topic archived. No new replies allowed.