Hi,

is it possible to control the output so that one output would replace the one before it ?

let me explain, i made a countdown timer, the counting instance is printed out but normally i would get something like this 9876543210 what i want is for the counting instance to replace the one before it, is that possible?

i thought about clearing the screen after each count but is there another way?

Member Avatar for iamthwee

You would have to clear the screen, and perhaps use the sleep function. That of course, makes your code non portable.

#include <windows.h>
#include <iostream>

using namespace std;

void clear_screen ( void );

int main()
{
     for ( int i = 9; i >= 0; i-- )
     {
       cout << i;
       Sleep(500);
       clear_screen();
     }
}

void clear_screen ( void )
{
  DWORD n;                         /* Number of characters written */
  DWORD size;                      /* number of visible characters */
  COORD coord = {0};               /* Top left screen position */
  CONSOLE_SCREEN_BUFFER_INFO csbi;

  /* Get a handle to the console */
  HANDLE h = GetStdHandle ( STD_OUTPUT_HANDLE );

  GetConsoleScreenBufferInfo ( h, &csbi );

  /* Find the number of characters to overwrite */
  size = csbi.dwSize.X * csbi.dwSize.Y;

  /* Overwrite the screen buffer with whitespace */
  FillConsoleOutputCharacter ( h, TEXT ( ' ' ), size, coord, &n );
  GetConsoleScreenBufferInfo ( h, &csbi );
  FillConsoleOutputAttribute ( h, csbi.wAttributes, size, coord, &n );

  /* Reset the cursor to the top left position */
  SetConsoleCursorPosition ( h, coord );
}

Or if you don't want the whole screen to clear, only the line with the number on it:

for ( int i=0; i<100; i++ ) {
  std::cout<< "\r" << i << "\t";
  Sleep( 100 );
}

\r goes to the start of the line, assuming you're printing to the start of the line, that is. The tab is just to assure that everything is cleared.

naw -- it isn't necessary to clear the whole cotten picken screen. All you have to do is output '\r' to move the cursor back to the beginning of the line. This assumes you want to display the value at the beginning of the line. putting it somewhere else on the screen is a little tricker because you have to move the cursor to a specific location.

for(int count = 0; count < maxcount; ++count)
{
    cout << '\r' <<  count;
    Sleep(1000);
}

[edit]twomers beat me to it :) [/edit]

Or you could output a couple of "\b" characters for even more control over the positioning.

Or you could output a couple of "\b" characters for even more control over the positioning.

what would the "\b" character do?

what would the "\b" character do?

Apparently you have never seen this list

> what would the "\b" character do?
Your first reaction on seeing something new should be to try and look it up for yourself, not throw it straight back at us.
For example, any C or C++ book worthy of the name should have a list of escape sequences.

thanks
i checked the link dragon posted , but still if i output to more than one line , how do i return to the first line ?
the "\r" character would only take me to the beginning of the current line, i'm just looking for the opposite of "\n" character .

there is no opposite of '\n'; If you want to move the cursor to another line you have to use some operating-specific cursor moving function. For MS-Windows console that means using some of the win32 api console functions (google for them). Or use a 3d party library such as the ncurses library which is a port from *nix curses. In any event there is no simple solution for that problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.