Problem with while loop

Hello,

I'm at the very beginning of learning C++.
I'm having a difficult time to understand why my while Loop doesn't work.

I wrote the following Code to better be able to explain the problem:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
int main()
{
    std::cout << "Hello World!\n";

    int loopTest = 1;

        while (loopTest = 1) {
            std::cout << "\nThis is the loop\n";
            std::cout << "Enter any number but 1 to exit the loop\n";
            std::cin >> loopTest;
            std::cout << loopTest << "\n"; //I added this line to check if the loopTest variable actually get's updated - It does.
        }
        std::cout << "This message never shows because program is stuck in loop\n";
}



Any advice is appreciated!

kind regards,
TheCodingSnake
Last edited on
loopTest = 1 assigns the value of 1 to loopTest

loopTest == 1 tests the value of loopTest, and if it's one, the result of the test is true.


Which of these did you use, and which did you mean?
while (loopTest == 1) {
}

fixes the code :D

Thank you so much!

kind regards,
TheCodingSnake

edit:
Actually I just noticed the code of my real Project already uses the == test . I'm going to sumbit the full code in my next post.

Last edited on
Row 12 and Row 170 are the problem.
I can't exit the loop except with the break function.

Isn't that exactly what I tried to show in the code above?
The loop shouldn't repeat unless adventureStatus == 1 right?

But despite that even when setting adventureStatus to 5 (or any number) at the very end of the loop it just repeats itself.


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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
#include <iostream>
#include <cstdlib>
#include <time.h>


//start main function

int main() {

	int adventureStatus = 1;

	while (adventureStatus == 1) { 

	int adventureStatus = 0;

	int userChoice1 = 0;
	int userChoice2 = 0;
	int userChoice3 = 0;
	int userChoice4 = 0;
	int userChoice5 = 0;

	//Choice 1
	if (adventureStatus != 1) {
		std::cout << "You're Frodo Baggins, 50 years old, currently attending a secret conference in Rivendell to decide on what to do with the Ring of the Dark Lord\n";
		std::cout << "Some of the most powerful people in Middle-earth are debating what to do with the one Ring.\n";
		std::cout << "Neither elves nor dwarfs are willing to trust the other side with the Ring.\n";
		std::cout << "You're just a hobbit and not quite sure what's going on\n\n";

		std::cout << "What do you do?\n";
		std::cout << "1) Wait for the others to come to a conclusion.\n";
		std::cout << "2) You're feeling hungry - Search for some breakfast. \n";
		std::cout << "3) Declare you're the one who takes the Ring!\n";

		std::cin >> userChoice1;

		if (userChoice1 == 1) {
			std::cout << "\n The conference doesn't reach a conclusion until Sauron's forces are at the gate of Rivendell.\n";
			std::cout << "\n Middle-earth is doomed\n\n";
			std::cout << "Do you want to restart the adventure?\n";
			std::cout << "1) Yes!\n";
			std::cout << "2) No.\n";
			std::cin >> adventureStatus;
			if (adventureStatus == 2) {
				break;
			}

		}
		else if (userChoice1 == 2) {
			std::cout << "The breakfast is delicious.\n";
			std::cout << "You kinda feel like you wasted precious time though\n";
			std::cout << "\n Middle-earth is doomed\n\n";
			std::cout << "Do you want to restart the adventure?\n";
			std::cout << "1) Yes!\n";
			std::cout << "2) No.\n";
			std::cin >> adventureStatus;
			if (adventureStatus == 2) {
				break;
			}
		}
		else if (userChoice1 == 3) {
			std::cout << "\nGandalf looks relieved but sad at the same time\n";
			std::cout << "Aragorn: You have my sword.\n";
			std::cout << "Legolas: And you have my bow.\n";
			std::cout << "Gimli: AND MY AXE!\n";
		}
		else {
			std::cout << "Invalid Input. Stop getting drunk in the Prancing Pony!\n";
			std::cout << "Do you want to restart the adventure?\n";
			std::cout << "1) Yes!\n";
			std::cout << "2) No.\n";
			std::cin >> adventureStatus;
			if (adventureStatus == 2) {
				break;
			}
		}
	}

		//Choice 2
		if (adventureStatus != 1) {
			std::cout << "You leave Rivendell and are on the way to Mordor!\n\n";

			std::cout << "Do you know in which direction Mordor is?\n";
			std::cout << "1) Of course! Right this way!\n";
			std::cout << "2) No, better ask Gandalf!\n";
			std::cout << "3) I'm feeling lucky";

			std::cin >> userChoice2;

			if (userChoice2 == 1) {
				std::cout << "\n You walk in the wrong direction!\n";
				std::cout << "\n Middle-earth is doomed\n\n";
				std::cout << "Do you want to restart the adventure?\n";
				std::cout << "1) Yes!\n";
				std::cout << "2) No.\n";
				std::cin >> adventureStatus;
				if (adventureStatus == 2) {
					break;
				}
			}

			else if (userChoice2 == 2) {
				std::cout << "\nHe tells you it's east!\n";
				std::cout << "Well done! A true hero knows when he needs help and is not embarassed to request it.\n";

			}
			else if (userChoice2 == 3) {
				std::cout << "The future of Middle-earth is now based on a coinflip...\n";
				std::cout << "A True hero would have asked for help!";
				srand(time(NULL));
				int direction = std::rand() % 2 + 1;
				if (direction == 1) {
					std::cout << "\n You walk in the wrong direction!\n";
					std::cout << "\n Middle-earth is doomed\n\n";
					std::cout << "Do you want to restart the adventure?\n";
					std::cout << "1) Yes!\n";
					std::cout << "2) No.\n";
					std::cin >> adventureStatus;
					if (adventureStatus == 2) {
						break;
					}
				}
				else {
					std::cout << "\n You walk in the right direction!\n";
					std::cout << "Sam is deeply impressed by your geographical knowledge!\n";
				}
			}
		}
		//Choice 3
		if (adventureStatus != 1) {
			std::cout << "Gimli wants to take a shortcut through the Mines of Moria.\n";
			std::cout << "Gandalf prefers to take the Pass of Caradhras.\n";
			std::cout << "The Gap of Rohan is also a possible route, but Aragorn thinks it's to close to Isengard\n";
			std::cout << "As the ring-bearer it's up to you to decide which path to take.\n";
			std::cout << "Which path do you want to take\n";
			std::cout << "1) The Pass of Caradhras\n";
			std::cout << "2) The Mines of Moria\n";
			std::cout << "3) The Gap of Rohan\n";
			std::cin >> userChoice3;

			while (userChoice3 == 1) {
				std::cout << "Saruman prevents you from passing over the mountains.\n";
				std::cout << "You have to turn around!\n";
				std::cout << "As the ring-bearer it's up to you to decide which path to take.\n";
				std::cout << "Which path do you want to take\n";
				std::cout << "1) The Pass of Caradhras\n";
				std::cout << "2) The Mines of Moria\n";
				std::cout << "3) The Gap of Rohan\n";
				std::cin >> userChoice3;
			}
			switch (userChoice3) {

			case 2:
				std::cout << "On the way through Moria a Balrog appears\n";
				break;
			case 3:
				std::cout << "\n Saruman catches you and steals the Ring\n";
				std::cout << "\n Middle-earth is doomed\n\n";
				std::cout << "Do you want to restart the adventure?\n";
				std::cout << "1) Yes!\n";
				std::cout << "2) No.\n";
				std::cin >> adventureStatus;
				if (adventureStatus == 2) {
					break;
				}

			
			
		}
		std::cin >> adventureStatus; // Even if I enter 5 here the while loop in row 12 repeats
	}

return 0;
}


Thank you for your help!

kind regards,
TheCodingSnake
Last edited on
That code doesn't build. The main function never closes. There is no final } . You've misaligned your code so you can't see that.
Hello TheCodingSnake,

A quick look at your full program I noticed this.
1
2
3
4
5
6
7
int main()
{
	int adventureStatus = 1;

	while (adventureStatus == 1)
	{
		int adventureStatus = 0;

Line 3 defines "adventureStatus" with the scope of "main".

Line 7 defines "adventureStatus" with the scope of the while loop. Defining the same variable name is allowed because they are in different scopes.

You should go back to your book and read up on scope or try these links
http://www.cplusplus.com/doc/tutorial/namespaces/
https://www.learncpp.com/cpp-tutorial/scope-duration-and-linkage-summary/
Line 7 defines "adventureStatus" with the scope of the while loop which works fine for the while loop until the end when you reach
std::cin >> adventureStatus; // Even if I enter 5 here the while loop in row 12 repeats . I believe that you want to change "adventureStatus" defined on line 3, but you are changing the variable defined on line 7. So your while condition is always seeing "adventureStatus" as having a value of (1) because it was never changed.

One other thing I changes is the {}s. When they line up in the same column it makes it easier to see what belongs to what.

Just to let you know it is referred to as the "Allman" style
https://en.wikipedia.org/wiki/Indentation_style#Brace_placement_in_compound_statements There are several different styles to choose from. The choice is yours, but IMHO the "Allman" style seems to be the easiest to read and follow. Which ever style you choose to use BE consistent in its use. Mixing styles makes the code harder to read.

Andy
@Repeater

I just noticed it too.
I'm struggling with the {}. Trying to practise new code with the "Allman" style now.

Thanks for your help!

@Andy

What you're telling me makes so much sense. It seems they're some massive gaps in my C++ knowledge as I never learned about global/local variables.

I'm gonna read up on it this weekend.

I also appreciate the information about Allman style. I try to keep my code organised but struggled with the placements of the {}, so I just let VS do it's thing.


I believe that you want to change "adventureStatus" defined on line 3, but you are changing the variable defined on line 7. So your while condition is always seeing "adventureStatus" as having a value of (1) because it was never changed.


Thanks to your help I figured out that had to remove the "int" in row 14. I want to change the global variable from row 10 to = 0. But by adding an "int" in front of it I declared a new variable instead changing the existing one.

Thank alot! Code works now the way I want it to do! :)

kind regards,
TheCodingSnake
Topic archived. No new replies allowed.