Connor
Connor

Reputation: 33

Unreachable goto statements

Im doing this test with this text based game and the inspect key and inspect door goto switches are are not working,it say unreachable code why is this happening

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

    inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }

Upvotes: 1

Views: 288

Answers (4)

Mohit
Mohit

Reputation: 11314

Say No To GOTO


This code block is causing this

enter image description here

    action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();
    
    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

it will only iterate between this. It will reach to line 22 and then will go back to line 16 to execute action: label.

Note:

Read these before going ahead

GOTO still considered harmful?

http://www.drdobbs.com/jvm/programming-with-reason-why-is-goto-bad/228200966

goto == bad programming

heavily nested loop == far worse programming

Don't use goto... fix your code!

enter image description here

Upvotes: -1

Mohit S
Mohit S

Reputation: 14064

You are suppose to write the conditions before your label. Thus your program would look like

action:
    Console.WriteLine("what do you want to do");
    string actionAnswer = Console.ReadLine();


    if((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
    {
        goto inspectSuroundings;
    }
    else if((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
    {
        goto inspectKey;
    }
    else if((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
    {
        goto inspectDoor;
    }
    else
    {
        Console.Beep();
        goto action;
    }

inspectSuroundings:
    Console.WriteLine("You see a small white room with a large two pronged key and a door.");
    goto action;

inspectKey:
    Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
    goto action;

inspectDoor:
    Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
    goto action;

But this is not the way to solve the situation you are coming up with you need to havae a loop instead of making GoTo. You can read more about 'goto' statement is bad practice and thus you can make your program like

while(true)
{
    Console.WriteLine("what do you want to do");
    string actionAnswer = Console.ReadLine();
    if((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
    {
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
    }
    else if((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
    {
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
    }
    else if((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
    {
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
    }
    else
    {
        Console.Beep();
    }
}

Upvotes: 0

oziomajnr
oziomajnr

Reputation: 1741

The problem is your logic! The following lines are the only ones that are executed.

action:
        Console.WriteLine("what do you want to do");
        string actionAnswer = Console.ReadLine();

    inspectSuroundings:
        Console.WriteLine("You see a small white room with a large two pronged key and a door.");
        goto action;

After this line, it keeps going to the action label. So the other part of your code would not be executed.

Also it is advisable not to use goto and label in C#. They can be replaced with conditional operations.

Upvotes: 1

Prajwal
Prajwal

Reputation: 4000

You are using goto action; in inspectSuroundings: which makes the following code unreachable.

inspectKey:
        Console.WriteLine("It seems to be a large gold key,with three prongs instead of two.");
        goto action;

    inspectDoor:
        Console.WriteLine("Its locked.There mus be a THREE PRONGED key around here.");
        goto action;

        if ((actionAnswer == "look") || (actionAnswer == "inspect") || (actionAnswer == "lookAround"))
        {
            goto inspectSuroundings;
        }else if ((actionAnswer == "inspectKey") || (actionAnswer == "lookAtKey"))
        {
            goto inspectKey;
        }else if ((actionAnswer == "inspectDoor") || (actionAnswer == "lookAtDoor"))
        {
            goto inspectDoor;
        }else
        {
            Console.Beep();
            goto action;
        }

I would suggest you to go through the Object Oriented Programming (OOP) method to write this program which would help you not only to understand and debug the program but also to develop a clean, less-error-prone program.

Upvotes: 0

Related Questions