I have written a simple program in C# in Visual Studio 2013. At the end of my program I instruct the user to:
"Please Press Enter to Exit the Program."
I would like to get the input from the keyboard on the next line and if ENTER is pressed, the program will quit.
Can anyone tell me how I can achieve this function?
I have tried the following code:
Console.WriteLine("Press ENTER to close console......");
String line = Console.ReadLine();
if(line == "enter")
{
System.Environment.Exit(0);
}
The input() function merely waits for you to enter a line of text (optional) till you press Enter. The sys. exit("some error message") is the correct way to terminate a program. This could be after the line with the input() function.
Try following:
ConsoleKeyInfo keyInfo = Console.ReadKey();
while(keyInfo.Key != ConsoleKey.Enter)
keyInfo = Console.ReadKey();
You can use a do-while too. More informations: Console.ReadKey()
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With