Trying to use a increment counter in a while loop

I am trying to use an increment counter in a while loop for multiple selections and my counter for the first expression entered is incrementing correctly, it is off by one. All other enteries are counted correctly.

I am also trying to count the total number of enters made and this counter is off by 2

any help would be greatly appriciated.

[code]
//This program tally a survey on beverages
#include <iostream>
using namespace std;

int main()
{
int number,choice,
person = 1 ;//Number of people taking survey
int sum1counter = 0,//Coffee
sum2counter = 0,//Tea
sum3counter = 0,//Coke
sum4counter = 0;//Orange Juice

cout << "Soft Drink Survey" << endl;
cout << endl;
cout << "\t1. Coffee" << endl;
cout << "\t2. Tea" << endl;
cout << "\t3. Coke" << endl;
cout << "\t4. Orange Juice" << endl;
cout << "\t5. Please enter -1 to exit" << endl;
cout << endl;

cout << "Please input the favorite beverage of person #" << person++ << ": \n";//Increment the number of people in the survey
cout << "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program." << endl;
cin >> choice;

while (choice != -1)
{
cout << "Soft Drink Survey" << endl;
cout << endl;
cout << "\t1. Coffee" << endl;
cout << "\t2. Tea" << endl;
cout << "\t3. Coke" << endl;
cout << "\t4. Orange Juice" << endl;
cout << "\t5. Please enter -1 to exit" << endl;
cout << endl;

cout << "Please input the favorite beverage of person #" << person++ << ": \n";//Increment the number of people in the survey
cout << "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program." << endl;
cin >> choice;

if (choice == 1)//Coffee
{
sum1counter++;
}
else if(choice == 2)//Tea
{
sum2counter++;
}
else if(choice == 3)//Coke
{
sum3counter++;
}
else if(choice == 4)//orangeJuice
{
sum4counter++;
}

}
cout << "The total number of people survery is " << person -2 << ". The results are as follows:" << endl;//Needed to correct the person running total with a -2
cout << "Beverage Number of Votes" << endl;
cout << "***********************************" << endl;
cout << endl;
cout << "Coffee " << sum1counter << endl;
cout << "Tea " << sum2counter << endl;
cout << "Coke " << sum3counter << endl;
cout << "Orange Juice " << sum4counter << endl;
cout << endl;


system("pause");

return 0;
}
Last edited on
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
//This program tally a survey on beverages
#include <iostream>
using namespace std;

int main()
{
	int number, choice = 0,
		person = 0;//Number of people taking survey
	int	sum1counter = 0,//Coffee
		sum2counter = 0,//Tea
		sum3counter = 0,//Coke
		sum4counter = 0;//Orange Juice

	while (choice != -1)
	{
		person++;
		cout << "Soft Drink Survey" << endl;
		cout << endl;
		cout << "\t1. Coffee" << endl;
		cout << "\t2. Tea" << endl;
		cout << "\t3. Coke" << endl;
		cout << "\t4. Orange Juice" << endl;
		cout << "\t5. Please enter -1 to exit" << endl;
		cout << endl;

		cout << "Please input the favorite beverage of person #" << person << ": \n";
		cout << "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program." << endl;
		cin >> choice;

		if (choice == 1)//Coffee
		{
			sum1counter++;
		}
		else if (choice == 2)//Tea
		{
			sum2counter++;
		}
		else if (choice == 3)//Coke
		{
			sum3counter++;
		}
		else if (choice == 4)//orangeJuice
		{
			sum4counter++;
		}
		else if (choice == -1)
		{
			person--;
		}

	}
	cout << "The total number of people survery is " << person << ". The results are as follows:" << endl;
	cout << "Beverage Number of Votes" << endl;
	cout << "***********************************" << endl;
	cout << endl;
	cout << "Coffee " << sum1counter << endl;
	cout << "Tea " << sum2counter << endl;
	cout << "Coke " << sum3counter << endl;
	cout << "Orange Juice " << sum4counter << endl;
	cout << endl;


	system("pause");

	return 0;
}


The reason you need to reduce the number of people by two was that your program was counting the person who entered "-1" as a person entering their favorite drink and you started the person count at 1 instead of zero.
Last edited on
Thanks for the help!!!
Topic archived. No new replies allowed.