Hi.
I would like the program to take the character/symbol/number right away after they had been pressed to avoid string being entered.

static void Main(string[] args)
        {
            char E;
            string response;

            do
            {
                Console.WriteLine("Please select your products :");
                Console.WriteLine("Oranges  - press O");
                Console.WriteLine("Apples   - press A");
                Console.WriteLine("Potatoes - press P");
                Console.WriteLine("To see your items press E.");
                response = Console.ReadLine();
                E = Convert.ToChar(response);

                switch (E)
                {
                    case 'O':
                        Console.WriteLine("You have ordered an Orange.\n");
                        break;
                    case 'A':
                        Console.WriteLine("You have ordered an Apple.\n");                        
                        break;
                    case 'P':
                        Console.WriteLine("You have ordered a Potato.\n");
                        break;
                    case 'E':
                        Console.WriteLine("Closing program.");
                        break;
                    default:
                        Console.WriteLine("Please select again.");
                        Console.WriteLine();
                        break;
                }
            }
            while (E != 'E');
        }

Recommended Answers

All 4 Replies

Try something like this. The true passed to ReadKey() suppresses the output of the character to the screen.

ConsoleKeyInfo a;
            do
            {
                 a= Console.ReadKey(true);
                switch(a.Key)
                {
                    case ConsoleKey.O : Console.WriteLine("You ordered an orange");
                        break;
                    case ConsoleKey.A: Console.WriteLine("You ordered an apple");
                        break;
                       //etc.
                }
            } while(a.Key != ConsoleKey.E);

Since ConsoleKey is an enum it is acceptable input to the switch.

Use c = Console.ReadKey(true);
true means, don't show char on console.
Remember c is not a char, but is of type ConsoleKeyInfo.

Edit: Jonsca was quicker :)

Wasn't trying to steal your thunder lol.

Thank you very much guys!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.