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:

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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <iostream>

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 ("<<op1<<" points)\n";
		cout<<"2. Tennis ("<<op2<<" points)\n";
		cout<<"3. Soccer ("<<op3<<" points)\n";
	}

	if (op1 > op2 && op1 > op3 && op2 < op3)
	{
		system("cls");
		cout<<"::Results::\n\n";
		cout<<"1. Rugby ("<<op1<<" points)\n";
		cout<<"2. Soccer ("<<op3<<" points)\n";
		cout<<"3. Tennis ("<<op2<<" points)\n";
	}

	if (op2 > op1 && op2 > op3 && op1 > op3)
	{
		system("cls");
		cout<<"::Results::\n\n";
		cout<<"1. Tennis ("<<op2<<" points)\n";
		cout<<"2. Rugby ("<<op1<<" points)\n";
		cout<<"3. Soccer ("<<op3<<" points)\n";
	}

	if (op2 > op1 && op2 > op3 && op1 < op3)
	{
		system("cls");
		cout<<"::Results::\n\n";
		cout<<"1. Tennis ("<<op2<<" points)\n";
		cout<<"2. Soccer ("<<op3<<" points)\n";
		cout<<"3. Rugby ("<<op1<<" points)\n";
	}

	if (op3 > op1 && op3 > op2 && op1 > op2)
	{
		system("cls");
		cout<<"::Results::\n\n";
		cout<<"1. Soccer ("<<op3<<" points)\n";
		cout<<"2. Rugby ("<<op1<<" points)\n";
		cout<<"3. Tennis ("<<op2<<" points)\n";
	}

	if (op3 > op1 && op3 > op2 && op1 < op2)
	{
		system("cls");
		cout<<"::Results::\n\n";
		cout<<"1. Soccer ("<<op3<<" points)\n";
		cout<<"2. Tennis ("<<op2<<" points)\n";
		cout<<"3. Rugby ("<<op1<<" points)\n";
	}

	if (op1 == op2 && op1 > op3)
	{
		system("cls");
		cout<<"::Results::\n\n";
		cout<<"There's been a tie!\n\n";
		cout<<"1. Rugby and Tennis both have "<<op1<<" points\n";
		cout<<"2. Soccer ("<<op3<<" points)\n";
	}

	if (op1 == op3 && op1 > op2)
	{
		system("cls");
		cout<<"::Results::\n\n";
		cout<<"There's been a tie!\n\n";
		cout<<"1. Rugby and Soccer both have "<<op1<<" points\n";
		cout<<"2. Tennis ("<<op3<<" points)\n";
	}

	if (op2 == op3 && op2 > op1)
	{
		system("cls");
		cout<<"::Results::\n\n";
		cout<<"There's been a tie!\n\n";
		cout<<"1. Soccer and Tennis both have "<<op2<<" points\n";
		cout<<"2. Rugby ("<<op1<<" points)\n";
	}

	total = op1+op2+op3;
	cout<<"\nThere has been a total of "<<total<<" votes!\n\n";

	cin.get();
	return 0;
}
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.

Cheers
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));


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.