I have this code working, for the most part.
The only problem I have is when I put in an incompatible answer after entering a compatible answer, the first time, the program terminates rather than resets.
The code takes either integers 1..4
for the answers, or -1
for the flag to terminate the sequence, and then cout
the results of the counts.
If not one of those five answers is entered, it is supposed to ask for an input again that is compatible.
If entering an input that is out of range once, at the start of the code, it will catch and run properly.
However, after inputting a correct answer once, if it receives an answer that is out of range or not -1
, the sequence stops rather than asking for a correct input.
#include <iostream>
using namespace std;
int main()
{
int coffee = 0;
int tea = 0;
int coke = 0;
int orangeJuice = 0;
int person = 1;
int choice;
cout << "Please input the favorite beverage of person #" << person << ": "
<< endl
<< "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program"
<< endl;
cin >> choice;
while (choice < 1 || choice > 4) {
cout << "Invalid choice" << endl;
cout << "Please input the favorite beverage of person #" << person << ": " << endl
<< "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" << endl;
cin >> choice;
}
while (choice != -1 && choice >= 1 && choice <= 4) {
if (choice == 1) {
person++;
coffee++;
cout << "Please input the favorite beverage of person #" << person <<
": " << endl
<< "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" << endl;
cin >> choice;
}
else if (choice == 2) {
person++;
tea++;
cout << "Please input the favorite beverage of person #" << person <<
": " << endl
<< "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" << endl;
cin >> choice;
}
else if (choice == 3) {
person++;
coke++;
cout << "Please input the favorite beverage of person #" << person <<
": " << endl
<< "Choose 1, 2, 3, or 4 from the above menu or -1 to exit the program" << endl;
cin >> choice;
}
else if (choice == 4) {
person++;
orangeJuice++;
cout << "Please input the favorite beverage of person #" << person <<
": " << endl
<< "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 surveyed is " << person << ". The results are as followed." << endl;
cout << "Beverage Number of Votes" << endl;
cout << "**********************************" << endl;
cout << "Coffee " << coffee << endl;
cout << "Tea " << tea << endl;
cout << "Coke " << coke << endl;
cout << "Orange Juice " << orangeJuice << endl;
return 0;
}
You are not doing any error handling to make sure that cin >> choice
is successful. If the user types in something that is not convertible to int
(like letters instead of numbers), operator>>
fails and sets error flags on the stream, choice
is either indeterminate or 0 (depending on implementation), and the bad input stays in the input buffer. Until you clear the error state and the input buffer, operator>>
will keep failing.
Also, your first while
loop is not allowing the user to enter -1
as an answer. It satisfies the choice < 1
condition that keeps the loop prompting the user for input.
Also, you just have a lot of redundancy in the way you handle input and output.
Try something more like this instead:
#include <iostream>
#include <limits>
using namespace std;
int main()
{
int coffee = 0;
int tea = 0;
int coke = 0;
int orangeJuice = 0;
int person = 0;
int choice;
do
{
cout << "Please input the favorite beverage of person #" << person+1 << ": " << endl;
do
{
cout << "Choose 1, 2, 3, or 4 from the above menu, or -1 to exit the program" << endl;
if (!(cin >> choice))
{
cout << "Invalid input" << endl;
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cin.clear();
continue;
}
if (((choice >= 1) && (choice <= 4)) || (choice == -1))
break;
cout << "Invalid choice" << endl;
}
while (true);
if (choice == -1)
break;
switch (choice)
{
case 1:
++coffee;
break;
case 2:
++tea;
break;
case 3:
++coke;
break;
case 4:
++orangeJuice;
break;
}
++person;
}
while (true);
cout << "The total number of people surveyed is " << person << ". The results are as followed." << endl;
cout << "Beverage Number of Votes" << endl;
cout << "**********************************" << endl;
cout << "Coffee " << coffee << endl;
cout << "Tea " << tea << endl;
cout << "Coke " << coke << endl;
cout << "Orange Juice " << orangeJuice << endl;
return 0;
}