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 << "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;
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;
}
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;
//This program tally a survey on beverages
#include <iostream>
usingnamespace 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++;
}
elseif (choice == 2)//Tea
{
sum2counter++;
}
elseif (choice == 3)//Coke
{
sum3counter++;
}
elseif (choice == 4)//orangeJuice
{
sum4counter++;
}
elseif (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.