Reputation: 91
When the user enters 3 I want the program to exit but instead the console says Press any key to continue then exits after the user presses any key. This is what i have for case 3. The entire switch statement is also wrapped in a do while statement.
do {
.
.
.
case 3:
Environment.Exit(0);
break;
} while (!isValid);
Upvotes: 3
Views: 11303
Reputation:
You should create a method which return bool and using it you can exit from loop.
bool bTry=true;
While(bTry)
{
..Your..
..Other..
..Code..
..
bTry=CanCountinue();
}
/// <summary>
/// Used to continue
/// </summary>
/// <returns>True if user want to continue
/// False if user want to exit</returns>
public bool CanCountinue()
{
Console.WriteLine("Enter Y to continue...");
String sAnother = Console.ReadLine();
return String.Compare(sAnother, "Y", true) ==0;
}
Upvotes: 0
Reputation: 28499
The message comes from visual studio when you start the application with ctrl+f5 (start without debugging). So, the application does terminate. It is only visual studio asking you for the key press so that you have a chance to see your console output.
The message will not appear when running the application from outside visual studio. It will also not appear when starting the debug version with f5 (console output is visible in the visual studio output log when debugging a console application, so there is no need to keep the console window open after the application has terminated).
Upvotes: 11