Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Printing on same line in c++

Tags:

c++

I am trying to write the time on the same line instead of it stacking the outputs. I can not seem to get it to work though.

Here is what I have: I thought the "\r" would make it reprint on the same line, but this doesn't work. And I also tried printf("\r"); and that didn't work either.

Can anyone help me figure out how to get this to work?

void countdown(int time)
{
    int h = (time / 3600);
    int m = (time / 60) - (h * 60);
    int s = time % 60;

    std::stringstream ss;
    ss << h << ":" << m << ":" << s;
    std::string string = ss.str();

    cout << "\r" << string << endl;
}
like image 507
Zach Starnes Avatar asked Feb 19 '14 03:02

Zach Starnes


People also ask

How do I print on the same line in printf?

printf("start:\n"); printf("\nline 1. printed");

How do I print two strings on the same line?

Use fgets() Instead Instead of using scanf for line input, use a line-oriented input function like fgets() which will consume an entire line (including the line ending). The ensures your input buffer is left in a consistent state that does not depend on the previous format specifier user, e.g.

How do you print a line in C?

Using %c to print the new line We can use the %c within the string in printf() statement with the value 0x0A, 10 or '\n'.

Is %d double in C?

%d stands for decimal and it expects an argument of type int (or some smaller signed integer type that then gets promoted). Floating-point types float and double both get passed the same way (promoted to double ) and both of them use %f .


4 Answers

I want to provide some useful information first.

You are inserting std::endl which prints next string on the next line.

std::endl is a newline \n followed by std::flush

The following newline \n and std::flush is equivalent to std::endl.

std::cout << printFunction() << '\n' << std::flush;

is just like

std::cout << printFunction() << std::endl;

Now removing std::endl will print the string in the same line.

cout << "\r" << string;
like image 71
Volkan Güven Avatar answered Oct 21 '22 17:10

Volkan Güven


cout << "\r" << string << endl;

endl moves the cursor to the next line. Try replacing it with std::flush which just ensures output's sent towards the terminal. (You should also #include <iomanip> and use std::setw(2) / std::setfill('0') to ensure the text you display is constant width, otherwise say the time moves from:

23:59:59

to

0:0:0

The trailing ":59" from the earlier time is not currently being overwritten or cleared (you could write a few spaces or send a clear-to-end-of-line character sequence if your terminal has one, but fixed-width makes more sense).

So ultimately:

std::cout << '\r'
          << std::setw(2) << std::setfill('0') << h << ':'
          << std::setw(2) << m << ':'
          << std::setw(2) << s << std::flush;
like image 41
Tony Delroy Avatar answered Oct 21 '22 19:10

Tony Delroy


try this:

cout << "\r" << string;

endl inserts a new-line character and flushes the stream.

like image 35
songyuanyao Avatar answered Oct 21 '22 17:10

songyuanyao


endl inserts a new-line character and flushes the stream.

cout << "\r" << string ; //works
like image 23
michaeltang Avatar answered Oct 21 '22 18:10

michaeltang