Console App - ReadLine() not capturing all the text.

lewis4521

New member
Joined
Jun 16, 2022
Messages
1
Programming Experience
Beginner
Hello,

I'm writing a failry basic console app. The essence of the code is that it loops through some values and asks the users to enter some data. To save the user having to type the same thing over and over again and have created mechanism where if they type '1' for example it will pre populate the console.

I do this by using this:

1:
console.write("some text")

The user can add some further text or just click enter to move on. This works well from an interface point of view, however a major problem is that I need to capture this value.

I do this by:
2:
string strValue = Console.ReadLine();

The problem is this is only capturing new text that is typed by the user, not the text thats been put there by the write() method.

Any suggestions how I can capture all the text please.

Thanks.
 
On the console, there are input and output streams: Console.In and Console.Out They are separate from each other. Console.Write() is actually a shortcut for Console.Out.Write() and Console.ReadLine() is a shortcut for Console.In.ReadLine().

As you've discovered, putting something into the output stream will not put it into the input stream.

The least hacky way that will work in any environment is to remember what you printed out and prefix whatever you get from reading with that remembered string. The hacky way that would probably only work on Windows is to use SendKeys() to yourself. But if you are using SendKeys(), you might as well be writing a Windows program instead of a console program.
 
Back
Top Bottom