• Advertisement
Sign in to follow this  

Having problem with if...else statement!

This topic is 4767 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
Quote:
Original post by Oluseyi
For me, I use whatever tool most appropriately solves as many of the problem constraints as possible.

...
Quote:
Original post by Oluseyi
I seldom use goto myself...


You seldom use it because it's seldom the best solution. Thus, goto is largely avoided, and even recommended against in many situations, including this one. Capiche?

Share this post


Link to post
Share on other sites
Quote:
Original post by Kwizatz
Just for reference: Go To Statement Considered Harmful
And I'm sure someone could provide a counter argument, as detailed to death in Code Complete, if I recall correctly.

Last I heard, it totally depends on the situation. Let sleeping dogs...

Share this post


Link to post
Share on other sites
Quote:
Original post by Oluseyi
Quote:
Original post by CJH
You seldom use it because it's seldom the best solution.
No. The situations that goto best solves seldom occur.

Because goto isn't an appealing solution of many applicable problems. Accept the Dogma, bitch. [smile]

To make a long story short, use goto when you must, but never else. Here, we musn't.

Share this post


Link to post
Share on other sites
Quote:
Original post by Brandon N
And I'm sure someone could provide a counter argument, as detailed to death in Code Complete, if I recall correctly.

Last I heard, it totally depends on the situation. Let sleeping dogs...


Oh I am sure of that as well, however, it was Dijkstra who postulated said "dogma", not quite any "dweeb" or "incompetent" in my opinion.

Share this post


Link to post
Share on other sites
This is not the place to have a flame war on whether gotos are harmful or not.

Share this post


Link to post
Share on other sites

This topic is 4767 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