Search code examples
c++console-applicationcarriage-return

C++ Console print on same line with carriage return


I have a console application which I want to print the progress with. However, to make this as nice as possible, I would like to print the percentage updates with a carriage return in order to keep the percentage updating instead of adding new lines with new progress status.

Printing with the carriage return works great, until I get a string that outranges the width of my console window. Obviously, the carriage return is not going to return to the start of the string which is longer than the console window.

Is there a possibility to catch this occurrence and somehow start at the beginning of the string again?

Visualized problem:

string = "This is a test string which is longer than the console";
  |<-      Console width       ->|
  |This is a test string which is|
->| longer than the console      |

The carriage return makes the string being printed starting from the -> as visualized above


Solution

  • The problem is that console windows differ. On the Windows platform you can adjust the width and height of the console.

    You may be able to find some API that will return the height and width of the console window; but there is no requirement for a platform to support that.

    There are libraries that can assist with cursor positioning. Search Software Recommendations (at StackExchange) to see what they recommend or search the internet for "c++ cursor position library".