Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear Console Buffer

I'm writing a sample console application in VS2008. Now I have a Console.WriteLine() method which displays output on the screen and then there is Console.ReadKey() which waits for the user to end the application.

If I press Enter while the Console.WriteLine() method is displaying then the application exits.

How can I clear the input buffer before the Console.ReadKey() method so that no matter how many times the user presses the Enter button while the data is being displayed, the Console.ReadKey() method should stop the application from exiting?

like image 475
Soham Dasgupta Avatar asked Sep 22 '10 13:09

Soham Dasgupta


People also ask

How do I clear terminal buffer?

Clear command is not the only way to clear the terminal screen. You can use Ctrl+L keyboard shortcut in Linux to clear the screen. It works in most terminal emulators. If you use Ctrl+L and clear command in GNOME terminal (default in Ubuntu), you'll notice the difference between their impact.

What does console clear () do?

The console. clear() method clears the console if the console allows it. A graphical console, like those running on browsers, will allow it; a console displaying on the terminal, like the one running on Node, will not support it, and will have no effect (and no error).

How do you clear the console in console?

This method clears the console and displays console was cleared message. Use the short cut Ctrl + L to clear the console. Use the clear log button on the top left corner of the chrome dev tools console to clear the console. On MacOS you can use Command + K button.

How do I clear the console in C++?

Just put system("cls"); line before you start printing anything.


1 Answers

Unfortunately, there is no built-in method in Console class. But you can do this:

while(Console.KeyAvailable) 
    Console.ReadKey(false); // skips previous input chars

Console.ReadKey(); // reads a char

Use Console.ReadKey(true) if you don't want to print skipped chars.

Microsoft References

  • Console.KeyAvailable

  • Console.ReadKey(bool)

like image 63
gandjustas Avatar answered Oct 23 '22 05:10

gandjustas