0

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');
        }

Edited by forneamax: n/a

3
Contributors
4
Replies
7
Views
7 Years
Discussion Span
Last Post by forneamax
Featured Replies
  • 1
    jonsca 1,059   7 Years Ago

    Try something like this. The true passed to ReadKey() suppresses the output of the character to the screen. [code] 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); [/code] Since … Read More

  • 2
    ddanbe 2,525   7 Years Ago

    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 :) Read More

1

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.

2

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 :)

Edited by ddanbe: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.