johnowen
johnowen

Reputation: 13

How to go back from a switch? c#

After I've added credits I want it to go back to display the main menu again. What is the way to do this? And is there an easier way? I am not very experienced in programming.

            Console.WriteLine("-------------------------------");
            Console.WriteLine("");
            Console.WriteLine("  VENDING MACHINIES LTD");
            Console.WriteLine("");
            Console.WriteLine("-------------------------------");

            Console.WriteLine("          Main Menu");
            Console.WriteLine("");
            Console.WriteLine("1 - Add Credits (current credits= 0.00)");
            Console.WriteLine("2 - Select product/s");
            Console.WriteLine("3 - Exit");
            Cons

Upvotes: 1

Views: 732

Answers (3)

Alexandros T
Alexandros T

Reputation: 36

You can use a recursive method and I would also suggest to extract the "menu" console write lines. Please have a look on the following:

static void Main(string[] args)
        {
            DisplayMenu();
            var choice = Convert.ToInt32(Console.ReadLine());
            // Final response
            choice = DisplayMenu(choice);
        }
        public static void DisplayMenu()
        {
            Console.WriteLine("          Main Menu");
            Console.WriteLine("");
            Console.WriteLine("1 - Add Credits (current credits= 0.00)");
            Console.WriteLine("2 - Select product/s");
            Console.WriteLine("3 - Exit");
            Console.WriteLine("");
            Console.Write("Please Enter A Number: ");
        }
        // Recursive overload 
        public static int DisplayMenu(int choice)
        {
            // If invalid input
            if (choice<1 || choice>3)
            {
                Console.Write("Incorrect option. Please Re-Enter: ");
                Console.WriteLine();
                DisplayMenu();
                return DisplayMenu(Convert.ToInt32(Console.ReadLine()));
            }
            // If valid show one more time the menu
            DisplayMenu();
            return Convert.ToInt32(Console.ReadLine());
        }

Upvotes: 0

Serge
Serge

Reputation: 43860

Try this code:

var  returnBack = true;
 do
{      
           Console.WriteLine("-------------------------------");
            Console.WriteLine("");
            Console.WriteLine("  VENDING MACHINIES LTD");
            Console.WriteLine("");
            Console.WriteLine("-------------------------------");

            Console.WriteLine("          Main Menu");
            Console.WriteLine("");
            Console.WriteLine("1 - Add Credits (current credits= 0.00)");
            Console.WriteLine("2 - Select product/s");
            Console.WriteLine("3 - Exit");
            Console.WriteLine("");
            Console.Write("Please Enter A Number: ");
            choice = Convert.ToInt32(Console.ReadLine());

            while (choice < 1 || choice > 3)
            {
                Console.Write("Incorrect option. Please Re-Enter: ");
                choice = Convert.ToInt32(Console.ReadLine());
            }


            switch (choice)
            {
                case 1:
                    Console.Write("How Many Credits Would You Like To Add? ");
                    credits = Convert.ToInt32(Console.ReadLine());

                    Console.Write("You Now Have {0} Credits", credits);
                    Console.WriteLine("");
                    Console.Write("Press return to go back to the main menu");
                    Console.ReadLine();
                    break;

                case 2:
                    Console.WriteLine();
                    break;
                case 3:

                returnBack=false;
                break;
               
            }
} while (returnBack);

Upvotes: 1

Lana
Lana

Reputation: 1046

You can use while(true) for eternity cycle of menu and return for exit from your application

Console.WriteLine("-------------------------------");
Console.WriteLine("");
Console.WriteLine("  VENDING MACHINIES LTD");
Console.WriteLine("");
Console.WriteLine("-------------------------------");

while(true)
{
    Console.WriteLine("          Main Menu");
    Console.WriteLine("");
    Console.WriteLine("1 - Add Credits (current credits= 0.00)");
    Console.WriteLine("2 - Select product/s");
    Console.WriteLine("3 - Exit");
    Console.WriteLine("");
    Console.Write("Please Enter A Number: ");
    choice = Convert.ToInt32(Console.ReadLine());

    while (choice < 1 || choice > 3)
    {
        Console.Write("Incorrect option. Please Re-Enter: ");
        choice = Convert.ToInt32(Console.ReadLine());
    }


    switch (choice)
    {
        case 1:
            Console.Write("How Many Credits Would You Like To Add? ");
            credits = Convert.ToInt32(Console.ReadLine());

            Console.Write("You Now Have {0} Credits", credits);
            Console.WriteLine("");
            Console.Write("Press return to go back to the main menu");
            Console.ReadLine();
            break;

        case 2:
            Console.WriteLine();

            break;
            
        case 3:
            Console.WriteLine("Bye-bye");

            return;
    }
}

Upvotes: 1

Related Questions