Help with the goto thing.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Oct 2008
Posts: 31
Reputation: tatainti55 is an unknown quantity at this point 
Solved Threads: 0
tatainti55 tatainti55 is offline Offline
Light Poster

Help with goto.

 
0
  #1
Oct 17th, 2008
Is there any other way to the same thing goto does?(You know what i mean i'm just too lazy too explain it right now >.<)
Goto messes everything up >.
For example, let's say i made a game that displays a menu, like:

"1. Play where is waldo
2.Quit"

Then you choose to play "Where is Waldo".
When you find him the program says "You won! and transports you back to the menu using goto.
But the next time you try to play "Where is Waldo" it'll say "You won!" again.
How can i fix that?
I'd like really simple answers, cuz kinda new to C++
And please make an example too.
Last edited by tatainti55; Oct 17th, 2008 at 6:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with the goto thing.

 
0
  #2
Oct 17th, 2008
My guess is that the program needs to reinitialize some variables. But you can almost always avoid using the goto statement at all
  1. bool done = false;
  2. int response;
  3.  
  4. while( !done )
  5. {
  6. response = menu();
  7. if( response == 2
  8. {
  9. done = true;
  10. }
  11. else
  12. {
  13. // initialize game variables here and
  14. // play game
  15. }
  16. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 31
Reputation: tatainti55 is an unknown quantity at this point 
Solved Threads: 0
tatainti55 tatainti55 is offline Offline
Light Poster

Re: Help with the goto thing.

 
0
  #3
Oct 17th, 2008
Oh yeah!
Can someone just give me a quick explanation of what bool does?
I kinda understand the conept of it but i don't know how to use it
And i didn't quite understand your explanation
Last edited by tatainti55; Oct 17th, 2008 at 6:13 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with the goto thing.

 
0
  #4
Oct 17th, 2008
bool is a variable that only contains 0 (false) or 1 (true). You use it only when you want something to be either true or false. I could also have written the while statement like this:
while( done == false)
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Help with the goto thing.

 
0
  #5
Oct 17th, 2008
bool is the boolean datatype. A variable declared as a bool can have 2 values true (= 1) or false (= 0).

Here is an explanation with an example.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 31
Reputation: tatainti55 is an unknown quantity at this point 
Solved Threads: 0
tatainti55 tatainti55 is offline Offline
Light Poster

Re: Help with the goto thing.

 
0
  #6
Oct 17th, 2008
Originally Posted by Ancient Dragon View Post
bool is a variable that only contains 0 (false) or 1 (true). You use it only when you want something to be either true or false. I could also have written the while statement like this:
while( done == false)
So kinda like:

  1. bool test;
  2. string input;
  3.  
  4. cout << "hi";
  5. cin >> input;
  6.  
  7. if(input==hi)
  8. test = 0
  9.  
  10. else
  11. test = 1
  12.  
  13. if(test = 1)
  14. cout << "poop";
  15.  
  16. else
  17. cout << "wow";
Is that the main idea of how it works?
Last edited by tatainti55; Oct 17th, 2008 at 6:47 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 305
Reputation: stilllearning has a spectacular aura about stilllearning has a spectacular aura about 
Solved Threads: 43
stilllearning stilllearning is offline Offline
Posting Whiz

Re: Help with the goto thing.

 
0
  #7
Oct 17th, 2008
not ... exaaactly !!

bool is a datatype .. so you cannot do bool == smthing

I don't know what you're trying to do, but here are some modifications.

  1. bool test;
  2. string input;
  3.  
  4. cout << "hi";
  5. cin >> input;
  6.  
  7. if(input==hi)
  8. test = true;
  9. else
  10. test = false;
  11.  
  12. if(test == true)
  13. cout << "poop";
  14. else
  15. cout << "wow";
Last edited by stilllearning; Oct 17th, 2008 at 6:46 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 31
Reputation: tatainti55 is an unknown quantity at this point 
Solved Threads: 0
tatainti55 tatainti55 is offline Offline
Light Poster

Re: Help with the goto thing.

 
0
  #8
Oct 17th, 2008
Originally Posted by stilllearning View Post
not ... exaaactly !!

bool is a datatype .. so you cannot do bool == smthing

I don't know what you're trying to do, but here are some modifications.

  1. bool test;
  2. string input;
  3.  
  4. cout << "hi";
  5. cin >> input;
  6.  
  7. if(input==hi)
  8. test = true;
  9. else
  10. test = false;
  11.  
  12. if(test == true)
  13. cout << "poop";
  14. else
  15. cout << "wow";
sorry, my bad
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,445
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1475
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Help with the goto thing.

 
0
  #9
Oct 17th, 2008
  1. bool test;
  2. string input;
  3.  
  4. cout << "hi";
  5. cin >> input;
  6.  
  7. if(input=="hi")
  8. test = false;
  9.  
  10. else
  11. test = true;
  12.  
  13. if(test == true)
  14. cout << "poop\n";
  15. else
  16. cout << "wow\n";
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 31
Reputation: tatainti55 is an unknown quantity at this point 
Solved Threads: 0
tatainti55 tatainti55 is offline Offline
Light Poster

Re: Help with the goto thing.

 
0
  #10
Oct 17th, 2008
Alright thanks
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC