3

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.

SirLeoncavallo
  • 119
  • 1
  • 9

1 Answers1

1

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);
Community
  • 1
  • 1
Яois
  • 3,838
  • 4
  • 28
  • 50
  • @chris you cannot test it in coliru (i tried in ideone too), but it works in a regular linux terminal. Same happens with some IDE integrated terminals (e.g. QtCreator's) – Яois Apr 19 '15 at 15:42
  • 2
    So it does. However, the code in the question works as well; the only difference is where the cursor ends up once the whole thing is over. And when it is shown, which is a question of flushing. – Wintermute Apr 19 '15 at 15:45
  • 1
    It's completely irrelevant whether you put it after each iteration or before each iteration. Other than for the very first iteration, and for any text following the last iteration, the result is exactly the same. The OP says it doesn't work _at all_ so this cannot be the problem. – Lightness Races in Orbit Apr 19 '15 at 15:47
  • Why the -1? You have to do the carriage return *after* introducing the new line, so it overwrites the previous one. – Яois Apr 19 '15 at 15:48
  • This is crazy. I am running the code right now (Linux terminal, as the OP said) and its obvious to me that it works when the carriage return is put at the beginning of the `cout` line. I don't understand the -1s but I hope (even if I'm wrong) that people know why are they downvoting. – Яois Apr 19 '15 at 15:53
  • I think you should compile and run the code in the question and reflect on the way in which its behavior doesn't differ from your proposal's. – Wintermute Apr 19 '15 at 16:03
  • @Wintermute if I run the OPs code, it prints nothing. If I run it in ideone, coliru or in an integrated terminal inside QtCreator or Eclipse, it shows what the OP says (multiple lines). I don't mind being downvoted, I would like why my solution wouldn't work in a regular Linux terminal! :D – Яois Apr 19 '15 at 16:09
  • The vote didn't come from me, anyway. I can guess whence it came. Anyway, the difference between your code and OP's is that OP's code ends with `\r`, placing the cursor back at the beginning of the line, whereas yours doesn't, placing the cursor at the end of the just-written iteration line. When the process ends, the terminal writes its prompt where the cursor happens to be, overwriting the iteration output in one case but not the other. You can make this more visible by putting `usleep(10000);` or so inside the loop. (and by also adding `cout.flush();`) – Wintermute Apr 19 '15 at 16:12
  • @Wintermute Yes, I did something like that.. placed a `sleep` and added an `std::flush` at the end of the line just in case. It works like charm (not if I leave the `\r` at the end though). – Яois Apr 19 '15 at 16:16
  • As i said in the comment to my question,the terminal is regular,no aspect,except for font and background colors,is customized. I forgot to say that i use eclipse as IDE. – SirLeoncavallo Apr 19 '15 at 16:19
  • 1
    Be sure you are **running the program from outside the IDE**, opening a new terminal and running the executable... – Яois Apr 19 '15 at 16:21
  • I think "Eclipse" is the missing piece in the puzzle -- Eclipse's internal console is somewhat...special. I wouldn't be at all surprised if it didn't support cursor repositioning. Does the same thing happen if you run the program in a regular terminal? – Wintermute Apr 19 '15 at 16:21
  • Yes,I run it outside the IDE.I've also tried to reboot,but nothing. – SirLeoncavallo Apr 19 '15 at 16:23