Search code examples
c#variablesgotocosmos

C# goto user input


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?


Solution

  • 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.. :)