Looping back C++

Hey so I'm making a little text based gladiator game and I'm trying to make it so that it asks you if you want to roll the dice again and if you do it should loop back to earlier in the code.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
      do{ cout << "Roll this dice. It'll decide your stength!" << endl;

                ~ unrelevant code snipped ~

		cout << "Do you want to roll the dice again?" << endl;
		cout << endl;
		cout << "1 - Yes" << endl;
		cout << "2 - No" << endl;
		cout << "Response: ";
		
		int choice;
		cin >> choice;
		cout << endl;
		cin.get();
		} while(choice==1); 

  cout << "You have decided to leave your stats as they are!" << endl;


How would I make it so that when the user types "1" it loops but if he type "2" it just continues?
Last edited on
i think you are doing correctly?
When I press 1 it doesn't loop back, the same with 2. I'm glad it doesn't for 2 but I need 1 to loop back.
You have to press enter after you have pressed 1.
I did but it still continues to

cout << "You have decided to leave your stats as they are!" << endl;

I don't have the slightest clue why it's doing this.
Last edited on
Here's a picture of it:

http://i.imgur.com/36ReW.jpg
The choice variable that is being used on line 15 is not the same as the choice variable defined on line 11. You have to define the choice variable outside the loop to be able to use the same variable both inside the loop body and the loop condition.
Last edited on
I already have

int choice;

outside the main function.
And I'm an idiot >_>

I had choice defined twice but because the later one was outside to loop, the variable was not the same as the one on line 11.

Thanks a lot Peter87



you can also screw 'Good Practice' and use the Goto statement

1
2
3
4
5
6
7
loop: // position u want code to loop back to.


if (choice==1)
{
goto loop;
}


hehe!
The less people that know about goto - the better!!
if goto is really that bad, why don't they just remove it completely. It has some advantage despite its funny disadvantage and most cases reduces complexity of code.
Because there is legacy code that still has goto in it. In C programming there is one valid place for a goto, that is to get out of deeply nested loops to do error processing. In C++ one would use exceptions.

So the use of goto's by newbies in C++, should be very strongly discouraged.
What if you want to break out of a deeply nested loop that is not an error? I have seen goto being suggested as a clean solution but I have never actually used it because goto is not part of my thinking.
Last edited on
closed account (zb0S216C)
TheIdeasMan wrote:
"In C programming there is one valid place for a goto, that is to get out of deeply nested loops to do error processing. In C++ one would use exceptions."


-1 That's abusing exceptions. Exceptions are used to allow the program to recover from a run-time failure and not to escape nested scopes. "goto", when confined to a very small area, is a good way to escape nested scopes, even in C++:

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
// This is bad:
struct escape_from_scope { };

try
{
    for(...)
    {
        for(...)
        {
            for(...)
                if(something) throw(escape_from_scope( ));
        }
    }
}

catch(escape_from_scope)
{
    // ...
}

// This is good (and cleaner):
for(...)
{
    for(...)
    {
         for(...)
             if(something) goto end_of_nest;
    }
}

end_of_nest:
// ... 

"goto" isn't evil; it just gets misused.

Wazzak
Last edited on
@Peter87
@Framework

OK, but would you agree that newbies shouldn't use goto, out of habit, when it would be more proper to use a loop?

I agree that goto is misused, and BridgesBenghazi somewhat comical post is a good example of that.
closed account (zb0S216C)
Yes, using "goto" to form a loop is a bad use, and yes, BridgesBenghazi's example is terrible (as was the joke).

Wazzak
Going back to the exceptions:

Framework wrote:
Exceptions are used to allow the program to recover from a run-time failure and not to escape nested scopes.


Well, exactly. The purpose of the exception in the nested loop is to process an error.

Can you (or anyone ) provide an example where there isn't an error, but one needs to jump out of all the loops?

I would have thought if there was no error, then the loops could complete naturally. If not, then perhaps it could solved with some redesign.

There was a long debate about goto's in the Lounge not long ago, hopefully we don't have to revisit all that again.

Another comment I have concerning goto's and infinite loops is that maybe it a case of saying one thing to beginners and the opposite to more experienced coders. I see this quite a bit in my sport of Ballroom Dancing. To a beginner, a coach might say: "Don't stand up too high on your toes, it will be easy to lose your balance." And to an advanced dancer: "You always have good balance, stand as high on your toes as you can."

Is this a fair analogy? I wonder if I can get my negative reputation (imaginary) point back? :+D

I wish we could all have real reputation points.
closed account (zb0S216C)
TheIdeasMan wrote:
"Can you (or anyone ) provide an example where there isn't an error, but one needs to jump out of all the loops?"

Let's assume you have a "std::vector" of 6 "char const * const"s (pardon the satanic practices), each of which points to a valid string, and let's also assume you're looking for the "cup" sub-string. You begin to iterate through the "std::vector" (outer-loop). For each iteration of the "std::vector", you enter a nested loop which searches the string for the aforementioned sub-string. If the 3rd string contained the "cup" sub-string, you'd break from all the loops because you've done what you needed; not because there's been an error.

TheIdeasMan wrote:
"I would have thought if there was no error, then the loops could complete naturally. If not, then perhaps it could solved with some redesign."

A loop does not always finish when it's supposed to. Sometimes, a loop can finish prematurely due to possible contributing factors -- such as threads or another process -- which are not forms of errors. If a loop ends before it's supposed to, it doesn't always mean its an error.

TheIdeasMan wrote:
"...I see this quite a bit in my sport of Ballroom Dancing. To a beginner, a coach might say: "Don't stand up too high on your toes, it will be easy to lose your balance." And to an advanced dancer: "You always have good balance, stand as high on your toes as you can."

Is this a fair analogy?"

That's not so much an infinite loop but contradictory statement. +1 for a good effort and for the unexpected Ballroom Dancing sport.

Wazzak
Last edited on
you'd break from all the loops because you've done what you needed; not because there's been an error.


And if this code was in function (which it probably should be), you could just return the answer from the function.

A loop does not always finish when it's supposed to. Sometimes, a loop can finish prematurely due to possible contributing factors -- such as threads or another process -- which are not forms of errors. If a loop ends before it's supposed to, it doesn't always mean its an error.


Again, could this be fixed with a return value that identified what happened?

That's not so much an infinite loop but contradictory statement. +1 for a good effort and for the unexpected Ballroom Dancing sport.


It wasn't meant to be an example of an infinite loop, but a comment about the type of advice to beginners versus advanced coders. You wouldn't advise beginners to use goto's & infinite loops whenever & however they liked. I am saying it would be better for beginners to avoid using these things at all - to start with. It is rather different for advanced coders who understand in which situations the use of these is OK.

Thanks for the positive feedback.
Topic archived. No new replies allowed.