console output - overwriting characters

V

Vasileios Zografos

Hi, I am trying to output some numbers on the console (command prompt)
using the std::cout command.

If I do the following:

for (int i=1;i<=10;i++)
std::cout<<i;

the output will be:

1 2 3 4 .... etc etc

is there a way to get the characters overwritten? For example, output:

1 then 2 then 3....etc etc so only one number at a time (something
like a countdown)


Thank you
V.Z.
 
B

BobR

Vasileios Zografos said:
Hi, I am trying to output some numbers on the console (command prompt)
using the std::cout command.
If I do the following:

for (int i=1;i<=10;i++)
std::cout<<i;

the output will be:

1 2 3 4 .... etc etc

is there a way to get the characters overwritten? For example, output:

1 then 2 then 3....etc etc so only one number at a time (something
like a countdown)
Thank you V.Z.

#include <iostream>
int main(){
for(int i(1); i <= 10; ++i){
// std::cout<< i <<" then "; // ha ha ha
std::cout<< i <<"\b"; // you might try this
} // for(i)
} // main()

That may or may not work depending on compiler/OS.

Do an online search (google) for 'ncurses'.
[ Homey don't play 'console'. ]
 
B

BobR

Ron AF Greve said:
Hi,
Use carriage return to go back to the beginning of the line. You might also
have to clean the line
string Clean( 20, ' ' );

cout << '\r' << i << Clean;

What are you talking about?
[ in other words, please stop top-posting. Put you answer *below* the
question. ]
 
J

James Kanze

Hi, I am trying to output some numbers on the console (command prompt)
using the std::cout command.
If I do the following:
for (int i=1;i<=10;i++)
std::cout<<i;
the output will be:
1 2 3 4 .... etc etc
is there a way to get the characters overwritten? For example, output:
1 then 2 then 3....etc etc so only one number at a time (something
like a countdown)

Not officially, in the language. The language 1) doesn't
require that cout be an interactive device, and 2) even for
interactive devices, allows hard copy devices to be used. (I
don't think that it would be too far off to say that this part
of the language was designed around systems using teletypes, and
hasn't changed since.)

In practice, if you're running on a modern general purpose
machine, and you're sure that std::cin is connected to a "tty
device", something along the lines of:

std::cout << "\r \r" ;

should clear the line and leave the cursor at the beginning,
supposing that the line doesn't contain more characters than you
have blanks in the above.
 
G

Gennaro Prota

In practice, if you're running on a modern general purpose
machine, and you're sure that std::cin is connected to a "tty
device", something along the lines of:

std::cout << "\r \r" ;

should clear the line and leave the cursor at the beginning,
supposing that the line doesn't contain more characters than you
have blanks in the above.

And one could know of how many characters are on the line by keeping
track of the count with a suitable filtering streambuf, right? :)
 
J

James Kanze

On 26 May 2007 17:41:44 -0700, James Kanze wrote:
And one could know of how many characters are on the line by keeping
track of the count with a suitable filtering streambuf, right? :)

Probably overkill. Typically, you're displaying percent in a
fixed format, and know exactly how many characters there are
anyway. In other cases, you can just count large: if you're
outputting bytes processed in a file, 20 or 30 should suffice.
 
G

Gennaro Prota

Probably overkill. Typically, you're displaying percent in a
fixed format, and know exactly how many characters there are
anyway. In other cases, you can just count large: if you're
outputting bytes processed in a file, 20 or 30 should suffice.

Yes, that was sort of a "fantasy", hence the conditional ("could") and
the smiley.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top