Help with goto statement

So I made this simple little game, but i want it to restart if you get it wrong so you can keep trying to guess the other 99 numbers and not rage quit too hard doing so. Is there a simple command to do this or will it have to be more difficult like goto's or loops?

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
  //Made by Logan M.
//1-100 game answer=42

#include <iostream>

int main() {
using namespace std;
system ("color 02");

	int number;
	cout<<"Enter a number between 1-100";
	cin>> number;
	cin.ignore();

	if (number == 42) {
		cout<<"YOU WIN\n";
		system("pause");
	}
	else if (number != 42) {
		cout<<"You lose try again! \n";
		system("pause");
	}


}
Last edited on
You can use a while loop to do that.

You would use it as the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

int main()
{
	bool repeat = true;
	while (repeat)
	{
		int number;
		std::cout << "ENTER NUMBER: ";
		std::cin >> number;

		if (number == 42)
		{
			std::cout << "YOU WON!" << std::endl;
			repeat = false;
		}
		else
		{
			std::cout << "INCORRECT, TRY AGAIN!" << std::endl;
		}
	}
	system("PAUSE");
	return 0;
}
^this, though I would use the do/while loop, since the code within the loop will execute at least once.
noxBox wrote:
I would use the do/while loop, since the code within the loop will execute at least once.


IMO, that is not sufficient reason to use a do loop. do loops can be error prone, K&R didn't like them. A slightly better reason to use them would be when a value in the condition can not be known at the start of the block statement. Even then it is possible to use a different form of loop, maybe at the price of an extra variable. All 3 forms of loop can be converted from one to another, and each can be written to execute 0,1 or as many times as one requires.
> do loops can be error prone, K&R didn't like them

Where has K&R expressed a dislike for do-while loops?

This is what they say in 'The C Programming Language (second edition)':
Experience shows that do-while is much less used than while and for. Nonetheless, from time to time it is valuable, as in the following function itoa ...
...
void itoa(int n, char s[])
...
The do-while is necessary, or at least convenient, since at least one character must be installed in the array s , even if n is zero.

That is in alignment with noxBox's contention: "since the code within the loop will execute at least once."

This is interesting; K&R usually do not put unnecessary braces around single statements; but in the case of the
do-while, they do, and go on to explain, after having stated earlier: "Experience shows that do-while is much less used than while and for."
We also used braces around the single statement that makes up the body of the do-while, even though they are unnecessary, so the hasty reader will not mistake the while part for the beginning of a while loop.
@JLBorges

Yep, I have been busted again :+) Dislike is much too strong compared with "much less used".

However, given that one type of loop can always be converted to another, I still hold with the idea that "just because it needs to execute at least once, that is not sufficient reason to use a do loop". For me, one exception is if a value in the condition cannot be known at the start of the loop. Or, something like K&R example of itoa() : "The do-while is necessary, or at least convenient, since at least one character must be installed in the array s , even if n is zero." To me, in other situations it just seems sensible to use a while loop.

I think the key difference in all this is that: The loop has to execute at least once; and the loop will execute once. I imagine that K&R would have only used a do loop if they had to - hence the "much less used" and "Nonetheless, from time to time it is valuable, .... "

Topic archived. No new replies allowed.