Having problem with if...else statement!

Started by
15 comments, last by Fruny 19 years, 3 months ago
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);
 }

Julio A. Cruz
Advertisement
restart:char c = getch();if((c == 'y')||(c == 'Y')){	goto restart;}exit(0);
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.
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.
Thanks!
Julio A. Cruz
Julio A. Cruz
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.
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.
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. ;-)
Quit screwin' around! - Brock Samson
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 caseif(again=="Y") {  goto restart; } else if(again=="N") {  exit(0); }
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.

This topic is closed to new replies.

Advertisement