user6567601
user6567601

Reputation: 31

How to get rid of goto's?

I'm new to programming and I would like to know if there are ways to not use gotos in my code here.

using System;

namespace project1

{
    class MainClass
{
    public static void Main()
    {
        start:

        Console.Clear ();

        Console.ForegroundColor = ConsoleColor.Green;

        int rannum;
        int input;

        Random r = new Random ();
        rannum = r.Next (1, 1001);

        Console.WriteLine ("Guess the number!");

        game:
        input = Convert.ToInt32 (Console.ReadLine ());

        if (input == rannum) 
        {
            Console.WriteLine ("Congrats!");
            Console.WriteLine ("Press any key to go again...");
            Console.ReadKey(true);
            goto start;
        } 
        else if (input <= rannum)
        {
            Console.WriteLine ("Guess higher!");
            goto game;
        } 
        else if (input >= rannum)
        {   Console.WriteLine ("Guess lower!");
            goto game;
        }
    }
}

I know that gotos are bad (apparently) so how do I do this without them?

Upvotes: 1

Views: 112

Answers (2)

user6567601
user6567601

Reputation: 31

I just did something like this and it's working, am I still doing anything wrong?

public static void Main ()
    {
        PlayGame ();

        Console.WriteLine ("Press 'n' key to exit the application, press any other key to play again.");
        string again = Convert.ToString(Console.ReadKey ());
        if (again == "n") {
            Environment.Exit (0);
        }

        PlayGame ();
    }

    public static void PlayGame ()
    {
        Console.Clear ();

        Console.ForegroundColor = ConsoleColor.Cyan;

        int randomNumber;
        int userInput;
        int gameover = 0;

        Random number = new Random ();
        randomNumber = number.Next (1, 1001);

        Console.WriteLine ("Guess the number!");

        while (gameover != 1) 
        {
            userInput = Convert.ToInt32(Console.ReadLine());

            if (userInput == randomNumber) {
                Console.Clear ();
                Console.WriteLine ("Congrats!");
                return;
            } else if (userInput <= randomNumber) {
                Console.Clear ();
                Console.WriteLine ("Guess Higher!");
            } else if (userInput >= randomNumber) {
                Console.Clear ();
                Console.WriteLine ("Guess Lower!");
            }
        }
    }

Upvotes: 0

Ben Voigt
Ben Voigt

Reputation: 283773

Jon commented while I was forming this answer, obviously great minds think alike.

Anyway, here's how you can eliminate the goto start:

public static void Main()
{
     Random r = new Random ();
     string again;
     do {
         PlayOneGame( r.Next(1, 1001) );
         Console.WriteLine("Play again?");
         again = Console.ReadLine();
     } while (!again.ToLower().StartsWith('n'));
}

public static void PlayOneGame(int rannum)
{
    Console.Clear ();

    Console.ForegroundColor = ConsoleColor.Green;

    int input;



    Console.WriteLine ("Guess the number!");

    game:
    input = Convert.ToInt32 (Console.ReadLine ());

    if (input == rannum) 
    {
        Console.WriteLine ("Congrats!");
        return;  // goes back to the caller, Main, but without using goto
    } 
    else if (input <= rannum)
    {
        Console.WriteLine ("Guess higher!");
        goto game;
    } 
    else if (input >= rannum)
    {   Console.WriteLine ("Guess lower!");
        goto game;
    }
}

Now, can you get rid of goto game in a similar way?

Note that I've also arranged things to only create one Random object and continue to use it no matter how many games are played -- it's not a good idea to keep creating new RNGs, the starting state is not terribly random.

Upvotes: 3

Related Questions