1

I'd like to make the C# console only accept input from the Enter key on the startup screen.

I've made it so that it closes the console when anything but the Enter key is pressed.

How can I make it so that the console only accepts input from the Enter key so that the application doesn't close when I press anything else, and then receive normal input afterwards?

class Program
{
    public static void ClearKeyBuffer()
    {
        while (Console.KeyAvailable)
            Console.ReadKey(true);
    }
    public static void Main (string[] args)
    {
        int attempts = 0;
        int displayattempts = 5;
        bool validentry;
        Console.WriteLine("Please press enter to begin");
        var key = System.Console.ReadKey(true);
        if (key.Key == ConsoleKey.Enter)
        {
            while (attempts < 5)
            {
                string input;
                attempts = (attempts + 1);
                Console.Clear();
                Console.WriteLine("Please wait...");
                Thread.Sleep(5000);
                Console.Clear();
                Console.WriteLine("Please enter your user number.");
                Console.WriteLine("Attempts Remaining:" + displayattempts);
                ClearKeyBuffer();
                Console.WriteLine(" ");
                input = Console.ReadLine();
                {
                    if (input == "5573")
                    {
                        validentry = true;
                    }

                    else
                    {
                        validentry = false;
                    }

                    if (validentry == false)
                    {
                        displayattempts = (displayattempts - 1);
                        Console.Clear();
                        Console.WriteLine("Error: Invalid number ID entered. Please wait 5 
                        seconds, and try again.");
                        Thread.Sleep(5000);
                    }

                    else if (validentry == true)
                    {
                        Console.Clear();
                        Console.WriteLine("Welcome Samuel");
                        ValidUserEntry();
                    }
                }
            }
        }
        if (displayattempts == 0)
        {
            Console.Clear();
            Console.WriteLine("Error: You have entered the wrong number ID too many times. 
             This system will now close in 5 seconds...");
            Thread.Sleep(5000);
            Environment.Exit(0);
        }
    }

    public static void ValidUserEntry()
    {
        ClearKeyBuffer();
        Console.Clear();
        Console.WriteLine("Please wait...");
        Thread.Sleep(5000);
        ClearKeyBuffer();
        Console.Clear();
        Console.WriteLine("What would you like to do?");
        Console.ReadLine();
    }
}

1 Answers1

4

Add this line before first if. Then remove if statement and the var key... line.

while (Console.ReadKey(true).Key != ConsoleKey.Enter);

Alternative, more verbose version:

ConsoleKeyInfo key;
do
{
    key = Console.ReadKey(true);
} while (key.Key != ConsoleKey.Enter);
Mustafa Arslan
  • 774
  • 5
  • 13
  • 1
    I've added a more verbose version to your answer because yours could be somewhat confusing to a beginner. I hope you don't mind :) – 41686d6564 stands w. Palestine Apr 20 '21 at 23:06
  • It's ok. Thanks :) – Mustafa Arslan Apr 20 '21 at 23:08
  • So just for clarification, you're storing what key was pressed in the key variable and using it per the condition that what key was pressed is the Enter key? And making it loop the startup screen until the condition that the Enter key is pressed. – samuelbachet Apr 20 '21 at 23:17
  • @samuelbachet That's exactly what happens in the second code block. The same happens in the first code block, except that the ConsoleKeyInfo value is not stored in a variable and it uses an _empty_ `while loop` which can be written as `while (someCondition) { }` or `while (someCondition) ;`. Here's a [relevant post](https://softwareengineering.stackexchange.com/q/419445/228458) about empty `while` loops. – 41686d6564 stands w. Palestine Apr 20 '21 at 23:24