SirLeoncavallo
SirLeoncavallo

Reputation: 119

Not able to make carriage return work on linux

i'm having problem with this code :

#include <iostream>
#include <math.h>
#include <unistd.h>
#include <string>
#include <iostream>
#include <stdio.h>

using std::cout; using std::cerr;
using std::cin;  using std::string;
using std::endl;

int main(int argc,char* argv[])
{
    for(int x = 0; x <= 2013; x++)
    {

        cout << "Iteration: "<< x << "\r";
        cout << "";

    }

    return 0;
}

i need my code to print "Iteration: 0" and than just refresh that 0 to 1,2,3,4.... everything on one console line. I used the carriage return but it doesn't work,the line are printed one after another as when i use "\n". The enviroment is linux 64 bit. The IDE is eclipse 8.01.

Upvotes: 3

Views: 1251

Answers (1)

Яois
Яois

Reputation: 3858

You have to put it at the beginning of the line:

cout << "\rIteration: "<< x;

EDIT: I have tested this modification of the original OP's code and it prints what he wants. Also, Oh dear look what I've found.

Also, as suggested by @Wintermute, you can do the following inside the for loop, for better visualization:

cout << "\rIteration: "<< x << std::flush;
sleep(1);

Upvotes: 1

Related Questions