Kevin Garnett MVP
Kevin Garnett MVP

Reputation: 77

goto - not within scope (C#)

I am very new to code. Can anyone in a simple way explain why I cant use the goto statement like this, to make the code start over again? Or, how this could have been done in the correct way? And also, why I get an error message on the use of "static". ** "No such label "Start" within the scope of the goto statmenet" "The modifier static is not valid for this item"

using System;


namespace ConsoleApp3
{
    class Program
    {
        static void Main(string[] args)
        {
            Start:

            Random numberGenerator = new Random();

            int num1 = numberGenerator.Next(1,11);
            int num2 = numberGenerator.Next(1, 4);


            Console.WriteLine("What is " + num1 + " times " + num2 + "?");


            int svar = Convert.ToInt32(Console.ReadLine());

            if (svar == num1 * num2)
            {
                Console.WriteLine("well done!");
            }
            else
            {
                int responseIndex = numberGenerator.Next(1, 4);

                switch (responseIndex)
                {
                    case 1:
                        Console.WriteLine("Wrong, try again? [Y or N]");
                        AskUser();
                        break;
                    case 2:
                        Console.WriteLine("The answer was incorrect");
                        AskUser();
                        break;
                    default:
                        Console.WriteLine("You can do better than that");
                        AskUser();
                        break;
                }



                 static void AskUser() {
                    string jaellernei = Console.ReadLine().ToUpper();
                    if (jaellernei == "Y")
                    {
                     goto Start;
                    } else
                    {
                        return;
                    } }
            }


        }
    }
}

Upvotes: 1

Views: 1566

Answers (3)

paparazzo
paparazzo

Reputation: 45096

You could do it like this

public static Random randd = new Random();
public static void FlachCards()
{
Start:

    if (AskAUser() == "Y")
    {
        goto Start;
    }

}
public static String AskAUser()
{
    Console.WriteLine("Enter Y to play again");
    return Console.ReadLine();
}

Upvotes: 0

Kaj
Kaj

Reputation: 814

Don't use goto unless you MUST to ! and As @Marc Gravell said, it's valid within a single method.

Alternatively : you can make a method of the code u used in the Main method, and then call it from both, main method and the other method where you used goto statement. Like :

 using System;
namespace ConsoleApp3
{
    class Program

    {

        static void Main(string[] args)
        {
             someFunction();
        }


        static void someFunction()
        {
            Random numberGenerator = new Random();

            int num1 = numberGenerator.Next(1, 11);
            int num2 = numberGenerator.Next(1, 4);


            Console.WriteLine("What is " + num1 + " times " + num2 + "?");


            int svar = Convert.ToInt32(Console.ReadLine());

            if (svar == num1 * num2)
            {
                Console.WriteLine("well done!");
            }
            else
            {
                int responseIndex = numberGenerator.Next(1, 4);

                switch (responseIndex)
                {
                    case 1:
                        Console.WriteLine("Wrong, try again? [Y or N]");
                        AskUser();
                        break;
                    case 2:
                        Console.WriteLine("The answer was incorrect");
                        AskUser();
                        break;
                    default:
                        Console.WriteLine("You can do better than that");
                        AskUser();
                        break;
                }
            }
        }

        static void AskUser()
        {
            string jaellernei = Console.ReadLine().ToUpper();
            if (jaellernei == "Y")
            {
                someFunction();
            }
            else
            {
                return;
            }
        }


    }
}

Upvotes: 0

Marc Gravell
Marc Gravell

Reputation: 1062945

Firstly, your AskUser method is incorrectly nested inside the other method - move it out.

Secondly: goto is only valid within a single method; you can jump around a single stack frame - you cannot jump between stack frames.

Thirdly: the number of times you should be using goto... well, it isn't quite zero, but it asymptotically approaches zero.

Upvotes: 6

Related Questions