Console line

Hello,

I'm new with C++ and doing while with trying, now I only have a question.
Is is possible that you rewrite the last line of the console?

I print the message to the console with the cout << command.
Is is possible that you rewrite the last line of the console?


What do you mean by 'rewrite'? Do you mean to delete it and write something different over it or just print out the same thing again on a different line?
Like if I have printed a line with the text:

Test: a Length: 9

that I can change the "a" to a "b" etc.
Ah, I see. No, it is not possible with just Standard C++, you will need to a get a library like ncurses or use OS dependent functions to do that.
But deleting the last line and write something different can?

I'm trying the lib of ncurses right now, but when you do endwin() it doesn't keep the text on the screen.
Well, the console was really only made to output logging information, not to make fancy CUIs. If you want to make a CUI, then you will have to go with ncurses. As for endwin() removing the text, you'll probably have to pause the program there (by getting input or something).
If you don't print the new line char, you could output a series of backspaces:

1
2
3
4
string str="Hello!";
cout << str;
cout << string(str.length(),'\b');
cout << "Hello again!";
@ Athar
That's a creative solution! And that's fully cross-platform and using standard functions.
You can also write a single carriage return to jump back to the beginning of the current line.

1
2
3
4
5
std::cout << "aaaaaa";
std::cout << '\r';
std::cout << "bbb"
std::cout << std::endl;
// output: bbbaaa 
Last edited on
@mtrenkmann: That only works on windows.
Using printf and '\r' that's the way i do it on linux also.
I stand corrected, but it is still not portable. I could create an OS that does nothing when you use \r but does both a newline/carriage return when you use a \n.
By your standard, cout is not portable, because some OS could very well not have an output stream, or even a screen.
Last edited on
Actually, cout would basically be a no-op in that case. Generally you shouldn't be using cout in serious programs anyway because someone could have redirected it to an error stream or a debug file.
I'm with RyanCaywood. With that reasoning, you could create an OS that prints "No memory for you!" to the screen whenever someone calls malloc() and then returns a null pointer. Would you say that calling malloc isn't portable?
All I am saying is that the effect of printing '\r' is implementation defined. I never said couting or using printf is not portable.
firedraco wrote:
because someone could have redirected it to an error stream or a debug file.

And why exactly is that supposed to be a problem?

firedraco wrote:
Generally you shouldn't be using cout in serious programs anyway

You might want to reconsider that statement.
Besides, what is your proposal for an alternative?
Topic archived. No new replies allowed.