1

I am using a chrono to get the time elapsed during a loop. I would like to display this time every loop run.

I can do this:

for(int i = 0;i<3;i++)
{
    sleep(2 secs);
    time= get_time();
    cout<<"time is : "<<time;
}

But I have the output:

time is : 2 time is : 4 time is : 6

I could add an endl to have it in column but that is not what I want. My loop is about million times, so I don't really want to print a million lines.

I would just like to print like:

time is : 2

and then refresh it to

time is : 4

and so on. Is there a way to do this?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Minamoto
  • 13
  • 6

2 Answers2

1

You can use endl with clrscr() .

Bertram Gilfoyle
  • 9,899
  • 6
  • 42
  • 67
1

printing to terminals is very easy, but it can be extremely hard at the same time. At its core, a terminal is simply a file, that you can use to write or read on. Performing tasks such as changing cursor's position is in fact system-specific and your code will have to be platform dependent.

But don't panic! People have done it before and even wrote libraries to do it. I think NCurses will do the job. https://www.gnu.org/software/ncurses/ncurses.html

I advice you to refer to this thread to see some issue related to your question: How to clear a specific line with NCurses?

I have never used ncurses my self, so I wish you best of luck!

Enjoy programming!

Community
  • 1
  • 1