drink survey

So I am able to run the program but it does not add up all the selections. Everything else works.

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
[//This program collects survey data from a few people and displays results 

#include <iostream>
# include <string>
#include <iomanip>

using namespace std;

int main()
{
	int coffee = 0, 
		tea = 0,
		coke = 0,
		juice = 0,
		person = 1,
		choice = 0;

	string statementA = "Please input the favorite beverage of person # ",
		statementB = "\nChoose 1,2,3 or 4 from the above menu or -1 to exit the program.";

	while (choice != -1)
	{
		//Display the drink survey and get a choice.
		cout << "Soft Drink Survey\n"
			<< "1. Coffee\n"
			<< "2. Tea\n"
			<< "3. Coke\n"
			<< "4. Orange juice\n"
			<< "5. Please enter -1 to exit.\n\n"
			<< statementA << person << statementB << "\n";
		cin >> choice;
		person = person++;
	}
	switch (choice)
	{
	case 1:
		coffee = (coffee ++);
		break;
	case 2:
		tea = (tea ++);
		break;
	case 3:
		coke = (coke ++);
		break;
	case 4:
		juice = (juice ++);
		break;
	}
	
	cin.get();

	if (choice = -1)
	{
		cout << "\n\nThe total number of people surveyed is " << (person - 2) << ".  The results are as follows:\n";
		cout << "Beverage of choice and votes.\n";
		cout << "*******************************\n";
		cout << "Coffee = " <<coffee << endl;
		cout << "Tea = " <<tea << endl;
		cout << "Coke = " << coke << endl;
		cout << "Orange juice = " << juice << endl;
	}

	cin.get();


	return 0;
}
Last edited on
closed account (3vX4LyTq)
You need to declare your variables as integers
Your while loop ends at line 33, therefore your switch statement is only executed once you exit the while loop.

Your syntax for incrementing variables is odd. You can increment a variable simply by:
 
  person++;


Line 52: You're using the assignment operator (=), not the equality operator (==). In fact, this if statement is unnecessary.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.

@ja r ed - What variables are not ints?

Last edited on
Your syntax for incrementing variables is odd.

It isn't just odd, I believe it produces undefined behavior because it changes the value of a variable twice without an intervening sequence point.
Ok I changed the person++ and the == I still get the same result Everything works fine exept it just wont give me the tally of the choices. It keeps track of the people survayed but the numbers come up as all zeros.
@AbstractionAnon
I changed all that you suggested

I got rid of the unnecessary if statement, changed the = to a ==, and person++

and still I get zeros for coffee tea ext....


not sure what to do other than scrap it and start with a while choice == -1 statement
Never mind I found my mistake.... Just had to move a couple of curly brackets around and it works!
Topic archived. No new replies allowed.