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.
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:
1 2 3 4 5 6 7
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.