Else statement not functioning properly in while loop

Hello and thank you for your time. I am a beginner and I am totally stuck. On line 126, the program is supposed to end but instead gives the output of "You got all possible points 12 out of 12." Why? I totally don't understand. Everything else in this program works fine.
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include "stdafx.h"
#include <iostream>
using namespace std;

void main()
{
	int input;
	int counter = 0;
	int life = 1;

	cout << "Hello. Press the number one to enter the program. Any other input will cause the program to terminate." << endl;
	cin >> input;

	if (input == 1)
	{
		cout << "This is a basic math quiz. Press 1, 2, 3, or 4 to enter your choice. Any other input will cause the";
		cout << " program to terminate." << endl;
		cout << "What is 5 + 4?\n 1) 6\n 2) 9\n 3)10\n 4)11\n";
		cin >> input;

		if (input == 2)
		{
			counter++;
			cout << endl << "Correct!";
		}
		else if ((((input == 1) || (input == 3) || (input == 4))) && cin.good())
		{
			cout << endl << "Incorrect!";
		}
		else
		{
			cout << "squiggle";
			exit(0);
		}

		cout << endl << "What is 5 * 2?\n 1) 10\n 2) 8\n 3) 12\n 4) 14\n";
		cin >> input;
		if (input == 1)
		{
			counter++;
			cout << endl << "Correct!";
		}
		else if ((((input == 2) || (input == 3) || (input == 4))) && cin.good())
		{
			cout << endl << "Sorry!";
		}
		else
		{
			cout << endl << "SquIIGLE 2";
			exit(0);
		}

		cout << endl << "What is 15 / 3?\n 1) 45\n 2) 15\n 3) 3\n 4) 5\n";
		cin >> input;
		if (input == 4)
		{
			counter++;
			cout << endl << "Correct!";
		}
		else if ((((input == 1) || (input == 2) || (input == 3))) && cin.good())
		{
			cout << endl << "Sorry!";
		}
		else
		{
			cout << "SQUiggggggggglekekeke33333";
			exit(0);
		}

		if (counter == 3)
		{
			cout << "You are eligible for the bonus round! ";
			cout << "There are a total of three questions. Any other value will still close program. \n";

			do
			{
				cout << "What is 2^3?\n 1) 4\n 2) 6\n 3) 8\n 4) 10\n";
				cin >> input;
				if (input == 3)
				{
					counter = counter + 3;
					cout << "Nice! Next question.";
				}
				else if ((((input == 1) || (input == 2) || (input == 4))) && cin.good())
				{
					life--;
					break;
				}
				else
				{
					exit(0);
				}

				cout << endl << "What is sqrt (81)?\n 1) 9\n 2) 10\n 3) 40\n 4) 3\n";
				cin >> input;
				if (input == 1)
				{
					counter = counter + 3;
					cout << "Nice! Next question.";
				}
				else if ((((input == 2) || (input == 3) || (input == 4))) && cin.good())
				{
					life--;
					break;
				}
				else
				{
					exit(0);
				}

				cout << endl << "What is 4.03 X 10^1?\n 1) 40.3\n 2) 4.03\n 3) 403\n 4) 1040\n";
				cin >> input;
				if (input == 1)
				{
					counter = counter + 3;
					cout << "You got all possible points " << counter << " out of " << counter << ".";
					life++;
				}
				else if ((((input == 2) || (input == 3) || (input == 4))) && cin.good())
				{
					life--;
					break;
				}
				else
				{
					exit(0);
				}
			} while (life == 1);
			if (life == 0)
			{
				cout << "You got " << counter << "points! Good job!";
				system("PAUSE");
			}
			else
			{
				system("PAUSE");
			}
		}
		else
		{
			cout << "Your score today was " << counter;
			cout << "! Great job! Have a good day! ";
			system("PAUSE");
		}
	}
	else
	{
		exit(0);
	}
}
Last edited on
well the answer to this line:
cout << endl << "What is 4.03 X 10^1?\n 1) 40.3\n 2) 4.03\n 3) 403\n 4) 1040\n";

is option 1, so it would then go on and print "You got all possible points ..."
It shows it when it receives a character even like ;. Its the only one to do that.
It shows it when it receives a character even like ;
Trying to enter a character when it expects int sets stream in failed state and cause all subsequent input to fail automaticly. If you are still using C++03, then value of variable will not change and will be used in all conditions it encounter. We could tell more what happens if you give us example of input which leads to this.
you didnt check cin.good()

which results in what miiniPaa wrote.
I'm not sure what you mean by "you didnt check cin.good()".

Any non-integer will cause this error.
Read on stream states and how formatted input work. For starters read these links:
http://www.parashift.com/c++-faq/stream-input-failure.html
http://www.parashift.com/c++-faq/istream-and-ignore.html
(and others atricles)
Topic archived. No new replies allowed.