C++ Change Output From "cout"
Asked Answered
U

3

5

Is it possible to change text printed with "cout"? I would like to make it show the current percentage of something without having to have a new line for each percentage. Is this possible?

Uri answered 8/12, 2010 at 14:34 Comment(4)
cout is not supposed to be an abstracton of a console, although most terminals will gladly interpret the carriage return character properly.Courier
you need to clarify your question, it's not clear what you're trying to achieve and what "change" means.Ustkamenogorsk
In general it is not possible. (imagine that the output from cout is fed directly to a printer. How would you instruct it to "unprint" the last line?) cout is an output stream, it makes no assumptions about which medium the output is sent to, or about the capabilities of that medium. Specific tricks can achieve what you want in some cases, but will fail horribly in others. If you want anything more dynamic than straight output of plain text, perhaps cout isn't the right tool to use.Flogging
std::cout does not assume that the output foes to the terminal. There are libs out there that understand the appropriate control sequence for terminals that allow you to manipulate the cursor position explicitly (Example ncurses). For simple one off applications that are doing simple output though '\r' should be enough.Enshroud
T
13

This works for me:

std::cout << "1111";
std::cout << "\r";
std::cout << "2222";

\r is a carriage return symbol. Puts the "cursor" back to the beginning of the line.

Alternatively you can use \b character. This is backspace. When printed it goes one character back.

Tagalog answered 8/12, 2010 at 14:38 Comment(5)
You have 3 \r (carriage return) more than you actually need (unless you meant \b, backspace)Numerical
The proper end-of-line symbol is "\n", not "\r". The standard library will interpret "\n" as CR-LF, LF-CR, or whatever is appropriate for your platform. There is also std::endl, which additionally forces flushing of the buffer.Ustkamenogorsk
@Gene Bushuyev: What are you talking about? OP asked for backspace or carriage return, to overwrite previous output.Tagalog
@Gene: \r is a line feed, \n is a new line. What the OP is asking for is how to overwrite data that is already on the console, not how to add new lines to it. \r will force the cursor back to the start of the current line (and future output will overwrite whatever was there). \b will erase the last character on the console and future output will replace it. Which is exactly what the OP was asking for.Aerobic
@Gene Bushuyev: Not quite correct. The iostream for files (cin/cout being types of file streams) convert the '\n' to/from a platform dependent end of line sequence. @Tagalog The interpretation of '\r' is dependent on the terminal being used. To get terminal independent cursor movement you need to use an appropriate lib that understands a terminals control sequence. But saying that '\r' should work in most places and is probably enough for the OP.Enshroud
F
2

In general it is not possible. (imagine that the output from cout is fed directly to a printer. How would you instruct it to "unprint" the last line?) cout is an output stream, it makes no assumptions about which medium the output is sent to, or about the capabilities of that medium. Specific tricks can achieve what you want in some cases, but will fail horribly in others. If you want anything more dynamic than straight output of plain text, perhaps cout isn't the right tool to use.

Flogging answered 8/12, 2010 at 15:48 Comment(0)
A
0

One thing that you will definitely not get from cout is terminal line length. As this can be changed, you might use too long lines, which (using '\r') will cause printing new lines every update. If you want to use specific platform then use platform-specific functions to obtain terminal size (mind that you might not be attached to any terminal at all, eg. redirected to file).

Alverta answered 8/12, 2010 at 20:2 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.