carriage return causing cout not to display

Hi everyone,

I am running ubuntu with code::blocks, and am trying to create an indicator that tells me about the progress of my function:

1
2
3
4
if (last_disp != time(0)) {
    last_disp = time(0);
    cout << "\rRunning... (step  " << t << " out of " << TIMESTEPS << ")";
}


For some reason, this does not work (displays nothing). If I replace /r with /n, it does work (proving that my use of the last_disp isn't somehow wrong). Am I doing something else wrong here?
Last edited on
I fixed it by adding << flush to my cout statements, but have no idea why it worked. Anyone?
wiki wrote:
[carriage return] commands a printer or other sort of display to move the position of the cursor to the first position on the same line.

http://en.wikipedia.org/wiki/Carriage_return

though I'm not sure why it wouldn't (in OP's case) print the "Running..." portion of the code :\
I'm well aware of what the carriage return does... As you said, that doesn't really explain why it wasn't being displayed.. Anyway I'm just using << flush now, although I don't really understand why it's necessary...
fyi: it works on my computer....

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
using namespace std;

int main(){

	int i = 10;

	cout << "\rbla bla bla " << i << " bla bla" << endl;

return 0;}

/*
bla bla bla 10 bla bla
*/


Topic archived. No new replies allowed.