Home > front end >  How to stop the console from scrolling when new text is added?(C/C )
How to stop the console from scrolling when new text is added?(C/C )

Time:12-19

Is there a way to stop my console application from scrolling when new text is added? In my application, I am adding new text and I don't want the console to scroll to the bottom each time that happens, I just want it to stay at the top the whole time.

This is for Windows, using the Win API.

CodePudding user response:

You can try writing system("cls"); to clear the screen and write from the top again.

CodePudding user response:

If you're using printf

  1. Do not end your lines with \n
  2. Start each line with \r. This will cause the program to continuously overwrite the line where you first started printing
  3. Add a fflush to flush the buffer

If you're using std::cout,

  1. Do not end your lines with std::endl
  2. Start each line wiht \r
  3. Add std::flush at the end of line

The only problem is that if the new line is shorter than the previous line, the text may not be overwritten.

  • Related