While loop with else statement

Forgive me, for I am only a noob.
This is what I've been working on for a couple days. Just started messing with programming this past week, reading tid-bits here and there to put something together.

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string word3;
	string word2 = "Yes";
	string word1 = "No";
	int much;
	while (1)
{
		cout << "Do you love me?\n( Yes or No )\n\n";
	cin >> word3;



	if (word3 == word2)
		{
			cout << "\nHow much?\n( 1 - 10 )\n\n";
			cin >> much;

			if (much <= 4)
			{
				cout << "\nLess or equal to 4.\n\n";
			}
			else if (much == 5)
			{
				cout << "\nEqual to 5.\n\n";
			}
			else if (much == 6)
			{
				cout << "\nEqual to 6.\n\n";
			}
			else if (much == 7)
			{
				cout << "\nEqual to 7.\n\n";
			}
			else if (much >= 8)
			{
				cout << "\nGreater or equal to 8.\n\n";
			}
			else
			{
				cout << "\nInvalid.\n\n";
			}
		}
	


	else if (word3 == word1)
	{
		cout << "\nThat's too bad..\n\n";
	}

	else
	{
		cout << "\nInvalid.\n\n";
	}
}
	return 1;
	}


My only issue is in the second set of if else statements. (where it asks "How much?") The final else is where I would want anything that doesn't trigger the if or if elses, to restart the program from the beginning. If you type any number after "How much?" the program displays what I want it to, then restarts at "Do you love me?". But if I don't enter a number, say a letter, it goes into endless loop, displaying this:

Do you love me?
( Yes or No )

How much?
( 1 - 10 )

Less or equal to 4.


And repeat.
How do I stop the infinite loop, and why did it start in the first place? I'm hoping it's a simple mistake since the else in the main function displays Invalid correctly, and although both elses are the same, (at least look the same, this is probably where I went wrong) it initiates the loop.
use break

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
#include <iostream>
#include <string>
using namespace std;
int main()
{
	string word3;
	string word2 = "Yes";
	string word1 = "No";
	int much;
	while (1)
{
		cout << "Do you love me?\n( Yes or No )\n\n";
	cin >> word3;



	if (word3 == word2)
		{
			cout << "\nHow much?\n( 1 - 10 )\n\n";
			cin >> much;

			if (much <= 4)
			{
				cout << "\nLess or equal to 4.\n\n";
				break;
			}
			else if (much == 5)
			{
				cout << "\nEqual to 5.\n\n";
				break;
			}
			else if (much == 6)
			{
				cout << "\nEqual to 6.\n\n";
				break;
			}
			else if (much == 7)
			{
				cout << "\nEqual to 7.\n\n";
				break;
			}
			else if (much >= 8)
			{
				cout << "\nGreater or equal to 8.\n\n";
				break;
			}
			else
			{
				cout << "\nInvalid.\n\n";
				break;
			}
		}
	
	else if (word3 == word1)
	{
		cout << "\nThat's too bad..\n\n";
		break;
	}

	else
	{
		cout << "\nInvalid.\n\n";
		break;
	}
}
	return 0;
	}
Thanks for the reply.
If I use break where you said, the infinite loop stops, but the loop for the whole main function stops too. So with various inputs this is what happens.

Do you love me?
( Yes or No )

No

That's too bad..



End of program.

Do you love me?
( Yes or No )

Yes

How much?
( 1 - 10 )

8

Greater or equal to 8.



End of program. If I use a letter in the "How much?" area (that is causing the entire issue in the first place) it looks like this

Do you love me?
( Yes or No )

Yes

How much?
( 1 - 10 )

g

Less or equal to 4.



End of program. If I put a break only after the trouble area:

1
2
3
4
5
6
7
8
9
else if (much >= 8) 
{
cout << "/nGreater or equal to 8./n/n";
}
else
{
cout << "/nInvalid./n/n";
break;
}


the loop remains exactly as it was, repeating:

Do you love me?
( Yes or No )

How much?
( 1 - 10 )

Less or equal to 4.



The loop also happens if I enter a number over 3,000,000,000, so it is using the else statement.
It might have something to do with my first set of if elses being words to words, and my second set being numbers to numbers. but there is an exception to both, because a number when it asks for a letter prints Invalid like it's supposed to, but a letter where it asks for a number prints a loop.
Last edited on
Step one in fixing your problem is to use an IDE that properly indents code or will fix the indentation for you at the press of a button. Code is meant to be -- first and foremost -- human readable. If your indentation is messed up, you are misleading yourself and your readers. Fix the indentation so that it is consistent. Use a well-known standard. Use only spaces. Never use tabs. Code indented with tabs rarely survives unscathed by copy/paste operations or different editors.

Start with this simple step and you will be far ahead many of the other students in your class.
The second step is to realize that you need to better constrain the else if (much >= 8) conditional. Otherwise 11, which is outside the range, is not invalid.

What really happens when you enter a value between 2^31 (2147483648) and 2^32-1 (4294967295)? The value in much is actually negative. This is the only way currently to enter the final else statement.

What happens when you use a letter? The value of much is not set. Because much is an int (an automatic POD type), its contents are undefined when you start. And when entering a letter, it will retain its last value. Also, and most importantly, at that point, cin is in an error state.
Last edited on
Thank you all for the replies, I'll research your suggestions and see what I come up with.
Topic archived. No new replies allowed.