How to fix invalid input in console application?

vitaminchik 486 Reputation points
2023-03-05T19:09:24.3566667+00:00

Hello. I wrote a program, but it doesn't work correctly. How can I fix the error? The user must enter variable values ​​for the quadratic equation. Then follow the arrows down (up) to other variables. Is it possible to correct the situation so that the end of the line at ReadLine is indicated not through Enter, but through an arrow.

Only after the user enters all the values ​​he should press Enter.

I am attaching a link to the program and an example of how the program should work.

Program

https://github.com/aminchikkk/ConsoleProgram/blob/main/PrintPoints.cs

Developer technologies C#
{count} votes

Accepted answer
  1. Minxin Yu 13,501 Reputation points Microsoft External Staff
    2023-03-06T07:33:38.18+00:00

    Hi, vitaminchik

    not through Enter, but through an arrow

    I'm afraid Console.ReadLine can't achieve what you want.

    If the standard input device is the keyboard, the ReadLine method blocks until the user presses the Enter key.
    The ReadLine method executes synchronously. That is, it blocks until a line is read or the <kbd>Ctrl+Z</kbd> keyboard combination (followed by <kbd>Enter</kbd> on Windows), is pressed.

    Update:

    It is a possibility. Use readkey instead of Console.ReadLine(); to read individual characters and then concatenate them. Below is a quick test,and the code is not optimized.

            public static void Print()
            {
                origRow = Console.CursorTop;
                origCol = Console.CursorLeft;
                ConsoleKeyInfo ki;
                PrintMenu();
                WriteAt(5, selectedValue + 1);
    
    
                String temp = string.Empty;
               do
                {
                    ki = Console.ReadKey();
    
    
                    switch (ki.Key)
                    {
                        case ConsoleKey.UpArrow:
                            {
                                variables[selectedValue] = temp;
                                introducedVariables[selectedValue] = variables[selectedValue];
                                if (selectedValue > 0)
                                    selectedValue--;
                            }
                            temp = string.Empty;
                            break;
                        case ConsoleKey.DownArrow:
                            variables[selectedValue] = temp;
                            introducedVariables[selectedValue] = variables[selectedValue];
                            if (selectedValue < strings.Length - 1)
                            {
                                selectedValue++;
                            }
                            temp = string.Empty;
                            break;
                        case ConsoleKey.Enter:
                            variables[selectedValue] = temp;
                            introducedVariables[selectedValue] = variables[selectedValue];
                            break;
                        default:
                            {
                                temp += ki.KeyChar.ToString();
                            }
                            continue;
                    }
    
                    Console.Clear();
                    PrintMenu();
                    WriteAt(5, selectedValue + 1);
                    
    
    
                }
                while (ki.Key != ConsoleKey.Enter) ;
    
    
                string line = new string('-', 50);
                Console.WriteLine(line);
            }
    
           protected static void WriteAt(int x, int y)
            {
                try
                {
                    Console.SetCursorPosition(origCol + x, origRow + y);
     
                }
                catch (ArgumentOutOfRangeException e)
                {
                    Console.Clear();
                    Console.WriteLine(e.Message);
                }
            }
    

    Best regards,

    Minxin Yu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.