exit console application code, also center code?

jacob_1988

Member
Joined
Mar 3, 2016
Messages
9
Programming Experience
Beginner
I have a few questions regarding a Menu with C# (as below).

The first problem is how do I write a line of code that will close the Console Application when prompted, i.e. if I write the message 'Do you want to quit (y/n) ?' - and they press Y - how would I write code that wouldexi the application?

My second problem is how would I center the whole menu at once? I can center one line at a time using the code below... but I need to center the whole menu?
            string textToEnter = "Menu";
            Console.SetCursorPosition((Console.WindowWidth - textToEnter.Length) / 2, Console.CursorTop);
            Console.WriteLine("1. - New Game");



            Console.Write("****************************");
            Console.WriteLine();
            Console.Write("Menu:");
            Console.WriteLine();
            Console.WriteLine("1. - New Game" +
            Environment.NewLine + "2. - Load Game " +
            Environment.NewLine + "3. - Options" +
            Environment.NewLine + "4. - Quit");
            Console.Write("**************************** \r\n");
            Console.WriteLine();
            Console.Write("Enter a number from the menu ");
            char answer = Console.ReadLine()[0];

            if (answer == '1')
            {
                Console.WriteLine("New Game will begin...");
            }

            if (answer == '2') 
            {
                Console.WriteLine("Loading Game...");
            }
           
            if (answer == '3')
            {
                Console.WriteLine("Options");
            }

            if (answer == '4')
            {
                Console.Write("Do you really want to quit the Game? (y, n): ");
                char YN = Console.ReadLine()[0];
           

            if (YN == 'y' ||
                YN == 'Y')
                    
            {
                    //char exitCode = Console.ReadLine()[0];
                    //Environment.Exit(0);
                    //Console.WriteLine("Environment.Exit");
                }
                else if (YN == 'n' ||
                     YN == 'N')
                {
                    Console.WriteLine("Continue Game...");
                }
           
            }
            else
            {
                Console.WriteLine("Enter numbers one to four only!");
            }
            Console.WriteLine();
 
Last edited by a moderator:
Firstly, if you have two separate questions then please create two separate threads. One thread per topic and one topic per thread. The fact that your questions relate to the same project doesn't make them part of the same topic.

With regards to the first question. You don't have to do anything to make an application exit, literally. A Console app will exit when the Main method completes so all you have to do is to not execute any more code. That might mean using a 'break' to jump out of a loop or whatever is appropriate in a specific situation.

As for the second question, you just have to do the same thing for each line. The logical thing to do is to write a method that accepts some text and writes it out centred, then call that method for each menu item.
 

Latest posts

Back
Top Bottom