Search code examples
c#loopsgotocontrol-structure

How to transfer the control of goto statement in C#


I'm beginner in programming and I'm trying this simple program of getting user name and sorting it and so on.

class Program
{
    static void Main(string[] args)
    {
        int number;
        dynamic y;

        string[] answer = new string[10];
        cases:
        Console.WriteLine("Enter the options given below 1.Add students\n 2.View all details\n 3.Sorting\n 4.Exit\n");
        int input = Convert.ToInt16(Console.ReadLine());
        switch (input)
        {
            case 1:
                Console.WriteLine("Enter the Number of Students to be added to the List");
                number = Convert.ToInt16(Console.ReadLine());
                for (int i = 0; i < number; i++)
                {
                    answer[i] = Console.ReadLine();
                }
            case 2:
                foreach (var item in answer)
                {
                    Console.WriteLine(item.ToString());
                }
                break;
            case 3:
                Array.Sort(answer);
                foreach (var item in answer)
                {
                    Console.WriteLine(item.ToString());
                }
                break;
            case 4:
                Console.WriteLine("Are you sure you want to exit");
                Console.WriteLine("1 for Yes and N for No");
                y = (char)Console.Read();
                if ((y == 1))
                {
                    goto cases;
                }
                else
                {
                    goto thankyou;
                }
                thankyou:
                Console.WriteLine("thank you");
                break;
        }
        Console.WriteLine("Are you sure you want to exit");
        Console.WriteLine("Y for Yes and 1 for No");
        y = (char)Console.Read();
        if ((y == 1))
        {
            goto cases;
        }
        else
        {
            goto thankyou;
        }
    }
}

My problem is that after every operation I have ask whether it should continue or not. I have added go-to statements but when pressed No its shows an exception for input variable that I have declared.

Can I use the go-to method or Is there any way we can do this ? It is the exception I'm getting Any suggestions what is wrong here??


Solution

  • If you want a loop in your program you should use one of the loop constructs in C#. In this case a while loop would work:

    bool keepPrompting = true;
    while(keepPrompting) {
         Console.WriteLine("Enter the options given below 1.Add students\n 2.View all details\n 3.Sorting\n 4.Exit\n");
        int input = Convert.ToInt16(Console.ReadLine());
    
        // The case statement on input goes here
    
         Console.WriteLine("Are you sure you want to exit");
         Console.WriteLine("Y for Yes and 1 for No");
         var y = (char)Console.Read();
         if (y != 'y') 
             keepPrompting = false;
    }
    
    Console.WriteLine("thank you");
    

    goto is almost never used in C# (or any other language) for that matter because it's hard to follow a program that can jump around to almost any location while a loop has a defined flow.