I am new to C#. I have a “menu” in a console app. Now when I have finished picking an item out of the menu and doing what that menu item require I want to loop and show the menu again so that the user can choose a diffenrent menu item. I have an exit on the menu and I only want to use that to exit. I have tried a while loop but this doesnt work. It closes the app after a menu item has been chosen and the chosen items code has run. What am I doing wrong?
static void Main()
{
int input = 0;
while (true)
{
Console.WriteLine("MENU");
Console.WriteLine("Please enter the number that you want to do:");
Console.WriteLine("1. Do thing A");
Console.WriteLine("2. Do thing B");
Console.WriteLine("3. Do thing C");
Console.WriteLine("4. Do thing D");
Console.WriteLine("5. Do thing E");
Console.WriteLine("6. Do thing F");
Console.WriteLine("7. Exit");
int menuchoice = int.Parse(Console.ReadLine());
switch (menuchoice)
{
case 1:
Console.WriteLine("Thing A has been done");
break;
case 2:
Console.WriteLine("Thing B has been done");
break;
case 3:
Console.WriteLine("Thing C has been done");
break;
case 4:
Console.WriteLine("Thing D has been done");
break;
case 5:
Console.WriteLine("Thing E has been done");
break;
case 6:
Console.WriteLine("Thing F has been done");
break;
case 7:
Environment.Exit; //edit
break;
default:
Console.WriteLine("Sorry, invalid selection");
break;
}
input++;
if (input < 30)
continue;
else
break;
}
}
Can anyone please help? Thanks in advance!
EDIT: I am aware that that “Console.Exit” would not work. I did just put it like that to illastrate that the console must exit there. My problem is that I need to loop the whole menu every time after a option have been chosen and the chosen option code has run. I only want to use the exit to exit. But at this point the menu does not loop, the console closes after just 1 option have been chosen and that options code has run.
EDIT: What happens when you start your program and you press 1 followed by Return? This is the real problem, the menu doesnt seem to be looping. After I start my program and press 1 followed by return the code in case 1 runs perfectly but then the console just closes. If I start the console again and press 2 this time the code in case 2 also runs perfectly but then the console closes again. I have tested all my cases like this and all of them runs perfectly.
Keep it simple: the loop stays in the loop until 7 is pressed