### Practice Problems - Jumping into C++

I purchased the Jumping into C++ book from cprogramming as suggested by a few people on the forums here. I'm at the end of Chapter 5 (Loops), Problem 7 (Page 81) where I have to make a poll, grab input and sum up the votes then show the results.

It mentions a graph but I'm not sure how to generate that, so I made it count the votes and display who wins in order of majority of votes.

I have a problem when I enter a letter as an option, it keeps looping indefinitely and I'm not sure what I'm doing wrong.

I would also like to know if there's a simpler way of doing this as it feels I've done a lot of unnecessary typing.

Here's the code:

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117`` ``````#include using namespace std; int main() { int op1 = 0, op2 = 0, op3 = 0; int choice, total; bool menu = true; while(menu) { system("cls"); cout<<"Poll - What sport do you prefer?\n\n"; cout<<"1. Rugby\n"; cout<<"2. Tennis\n"; cout<<"3. Soccer\n"; cout<<"4. Exit (Stop Voting)\n\n"; cin>>choice; cin.ignore(); if (choice != 1 && choice != 2 && choice != 3 && choice != 4) cout<<"You must enter a choice between 1-4\n\n"; if (choice == 1) op1++; if (choice == 2) op2++; if (choice == 3) op3++; if (choice == 4) menu = false; } if (op1 > op2 && op1 > op3 && op2 > op3) { system("cls"); cout<<"::Results::\n\n"; cout<<"1. Rugby ("< op2 && op1 > op3 && op2 < op3) { system("cls"); cout<<"::Results::\n\n"; cout<<"1. Rugby ("< op1 && op2 > op3 && op1 > op3) { system("cls"); cout<<"::Results::\n\n"; cout<<"1. Tennis ("< op1 && op2 > op3 && op1 < op3) { system("cls"); cout<<"::Results::\n\n"; cout<<"1. Tennis ("< op1 && op3 > op2 && op1 > op2) { system("cls"); cout<<"::Results::\n\n"; cout<<"1. Soccer ("< op1 && op3 > op2 && op1 < op2) { system("cls"); cout<<"::Results::\n\n"; cout<<"1. Soccer ("< op3) { system("cls"); cout<<"::Results::\n\n"; cout<<"There's been a tie!\n\n"; cout<<"1. Rugby and Tennis both have "< op2) { system("cls"); cout<<"::Results::\n\n"; cout<<"There's been a tie!\n\n"; cout<<"1. Rugby and Soccer both have "< op1) { system("cls"); cout<<"::Results::\n\n"; cout<<"There's been a tie!\n\n"; cout<<"1. Soccer and Tennis both have "<
When you enter a letter, it tries to interpret it as a number, and of course it can't, so it puts std::cin into an error state. When std::cin is in this error state it no longer tries to perform input, and so you get default values from input everywhere until you clear the error state.

Consider this technique of input:
 ``1234567`` ``````int Input = -1; do { std::cin.clear(); std::cin.sync(); std::cout << "Please enter a valid number: "; }while(!(std::cin >> Input));``````
This seems to work for the first time round inputting a character, it then waits for 2 inputs to proceed adding to the poll. If I input a number and then a letter it loops indefinitely as well.

What I wanted to know was if there was a function to check if input is an integer, a string or any type and what method I could use to simplify this program so there isn't so much typing.

Cheers
Consider this technique of input:
 ``1234567`` ``````int Input = -1; do { std::cin.clear(); std::cin.sync(); std::cout << "Please enter a valid number: "; }while(!(std::cin >> Input));``````

Just like to note that this method is not guaranteed to remove the unwanted input from the input stream, so it should be avoided.

Using ignore is canonical.
Topic archived. No new replies allowed.