Jump to content
  • Advertisement
Sign in to follow this  
drjulio2002

Having problem with if...else statement!

This topic is 5040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hello Everyone! I have this problem with my guessing game that I ask the player if he or she wants to play again, but when the player says yes the program ends and doesn't start again:
if(again=="y" || again=="Y")
 {
  goto restart;
 }
 else if(again=="N" || again=="n")
 {
  exit(0);
 }

Share this post


Link to post
Share on other sites
Advertisement
Quote:
Original post by drjulio2002
Hello Everyone!

I have this problem with my guessing game that I ask the player if he or she wants to play again, but when the player says yes the program ends and doesn't start again:

*** Source Snippet Removed ***


It is highly recommended not to use 'goto'. Instead you should make function that are called so you can easily control what is executed. Here is a simple pseudo-coded model for Tic Tac Toe:


Display Title()
while( !Exit )
{
ResetGame()
while(!GameOver)
{
Player1Move()
Player2Move()
CheckForGameOver()
}
AskToQuit()
}


AskToQuit is the function that will contain the code that determines whether or not the program should exit - if they say they want to play again, you simply set Exit = 0, so the loop restarts - making a new game. If they choose no - the while loop exits and the program is over.

Share this post


Link to post
Share on other sites
you are trying to match the char variable "again" to a string literal. This can work in C++ if you use a string class like std::string because it overloads the == operator. Otherwise you should follow mike25025's example and match to a char 'Y' instead and all will be okay.

Share this post


Link to post
Share on other sites
Quote:
Original post by Drew_Benton
It is highly recommended not to use 'goto'.
By dweebs, incompetents and the same people who recommend not to use continue and break. Use it when it makes sense, and when you don't cross declaration/initialization statements as a consequence.

Share this post


Link to post
Share on other sites
goto is really not that bad to use, but absolutely not necessary and makes code much less readable.
I don't use it much more as a design option, there are cleaner ways to do it IMHO.

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by Drew_Benton
It is highly recommended not to use 'goto'.
By dweebs, incompetents and the same people who recommend not to use continue and break. Use it when it makes sense, and when you don't cross declaration/initialization statements as a consequence.

Although I've seen goto statements in places that make sense and minimize "speghetti code" I can always easily find other methods that are slightly more readable. The last time I used one was when I was 15 and that was about 14 years ago. I've been able to live half of my life without using one, thereby ruling out the necessity. But my point is that goto's are just bad practice. I've known other programmers who write wonderful code using occasional goto statements BUT they always get lazy at some point and throw in a sloppy goto.

I know there are programmers out there that have the discipline to tame the evil goto... but why bother? It's just one less thing I have to look for when I'm debugging your code.

(BTW) I'm not a dweeb, FAR from incompetent and I use continues and breaks. ;-)

Share this post


Link to post
Share on other sites
Goto makes things much more readable when you're using it to break out of several loop layers at once, but just so that this won't derail the thread I suggest this to Dr.Julio:

Change this:

if(again=="y" || again=="Y")
{
goto restart;
}
else if(again=="N" || again=="n")
{
exit(0);
}

To this:

toupper(again); //makes it always upper case

if(again=="Y")
{
goto restart;
}
else if(again=="N")
{
exit(0);
}

Share this post


Link to post
Share on other sites
I seldom use goto myself, but I'm not a fan of dogma. For me, I use whatever tool most appropriately solves as many of the problem constraints as possible. By this I mean not only solving the algorithmic problem, but being readable, comprehensible and maintainable, among other possible constraints.

Share this post


Link to post
Share on other sites

This topic is 5040 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Guest
This topic is now closed to further replies.
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!