1

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;
        }
Connor
  • 33
  • 2
  • 2
    Don't use `goto`. Never ever use `goto`. – Prajwal Nov 25 '16 at 06:03
  • 2
    What I can see is, Your code is like a loop `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;` – Ankit Nov 25 '16 at 06:08
  • 1
    @Prajwal There are uses for goto, so that advice is not valid. But usually it's not needed. As for the question: debug line by line and you'll see. In this case don't use goto and write proper code, for example with switch/case – Sami Kuhmonen Nov 25 '16 at 06:09
  • There is no condition to omit `got action;` so it will always redirect it to label – Ankit Nov 25 '16 at 06:09
  • @SamiKuhmonen , I know. But for somebody at this level, it is better to advice the against. Also, `goto` makes code much less readable. **my opinion – Prajwal Nov 25 '16 at 06:11
  • @SamiKuhmonen there are very, very, very, very few use cases where goto is *one* possible solution. but in 99.99999....% of all occurances, GOTO should GOAWAY. like this one. – Franz Gleichmann Nov 25 '16 at 06:18

4 Answers4

1

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.

oziomajnr
  • 1,671
  • 1
  • 15
  • 39
0

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.

Prajwal
  • 3,930
  • 5
  • 24
  • 50
0

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

Community
  • 1
  • 1
Trikaldarshiii
  • 11,174
  • 16
  • 67
  • 95
0

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();
    }
}
Community
  • 1
  • 1
Mohit S
  • 13,723
  • 6
  • 34
  • 69