-5

I am making an OS with Cosmos and want to use goto to go to the user input but I am getting the error

No such label 'input' within the scope of the goto statement

'input' is a variable in which the user has inputted.

I can understand why this is happening but how do I fix it?

Ian Stegner
  • 23
  • 1
  • 1
  • 6

2 Answers2

1

You cannot user variables as scope identifier for goto statement.. you have to use label identifier within scope (namespace) indicating it by ":" ..

for example

using System;

class Program
{
    static void Main()
    {
        Console.WriteLine(M());
    }

    static int M()
    {
        int dummy = 0;
        for (int a = 0; a < 10; a++)
        {
            for (int y = 0; y < 10; y++) // Run until condition.
            {
                for (int x = 0; x < 10; x++) // Run until condition.
                {
                    if (x == 5 &&
                        y == 5)
                    {
                        goto Outer;
                    }
                }
                dummy++;
            }
        Outer:
            continue;
        }
        return dummy;
    }
}

method M contains three nested loops. The first loop iterates through numbers [0, 9], as do the two inner loops. But in the third loop, a condition is checked that causes the loop to exit using the break keyword. For Break

The code increments the dummy variable after each completion of the inner loop. If the inner loop is exited early, this variable should be left alone. With the goto statement, it is not incremented. Result: The value 50 is printed to the console. The int is incremented 10 x 5 times. However: If the goto was a break, the result would be 10 x 10 times, or a total of 100.

Hope this Help.. :)

Abhinav
  • 1,202
  • 1
  • 8
  • 12
  • Variables are not for jumping or scope identifier... it makes no sense. Consider rechecking your requirement and architecture... :) – Abhinav Feb 13 '17 at 08:53
0

I am making an OS with Cosmos

For getting any remotely useful answers, I think you will have to give some information about the scope of the OS. Are you only fiddling around with COSMOS a bit, or do you have some special use-case you want to serve with a custom COSMOS OS?

and want to use goto to go to the user input

Especially in the latter case (specialized OS) you should clearly refrain from using GOTO, unless you have a very good reason to do so (and in my humble opinion there is no such thing as a really good reason to use GOTO). There are viable alternatives to GOTOs in modern programming languages and you should re-think your design, algorithm, whatsoever.

To answer your question. Here is an example that produces the very error message you are experiencing

private void FirstMethod()
{
    goto MyLabel;
}

private void SecondMethod()
{
    MyLabel:
    return;
}

I have defined a label in Method. Anyway, from Main you cannot simply jump from main to another method, since the compiler would not know where to return to, after the method has finished, since no data would have been pushed to the call stack on GOTO (please see the Wikipedia page about the call stack for further information).

The following, anyway, would work, since the label and the GOTO live within the same scope

void MyMethod()
{
    goto MyLabel;
    // do something
    MyLabel:
    return;
}
Paul Kertscher
  • 9,416
  • 5
  • 32
  • 57