By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
425,587 Members | 1,891 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 425,587 IT Pros & Developers. It's quick & easy.

console output - overwriting characters

P: n/a
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.

--
Posted via a free Usenet account from http://www.teranews.com

May 26 '07 #1
Share this Question
Share on Google+
8 Replies


P: n/a

Vasileios Zografos <no***@nowhere.netwrote in message
news:46***********************@free.teranews.com.. .
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'. ]
--
Bob R
POVrookie
May 26 '07 #2

P: n/a
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;
Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"Vasileios Zografos" <no***@nowhere.netwrote in message
news:46***********************@free.teranews.com.. .
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.

--
Posted via a free Usenet account from http://www.teranews.com

May 26 '07 #3

P: n/a

Ron AF Greve <ron@localhostwrote in message ...
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. ]

May 26 '07 #4

P: n/a
On May 26, 8:21 pm, Vasileios Zografos <n...@nowhere.netwrote:
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 27 '07 #5

P: n/a
On 26 May 2007 17:41:44 -0700, James Kanze wrote:
>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? :-)

--
Gennaro Prota -- C++ Developer, For Hire
https://sourceforge.net/projects/breeze/
May 27 '07 #6

P: n/a
On May 27, 1:27 pm, Gennaro Prota <address@spam_this.comwrote:
On 26 May 2007 17:41:44 -0700, James Kanze wrote:
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? :-)
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.

--
James Kanze (Gabi Software) email: ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 27 '07 #7

P: n/a
On 27 May 2007 12:58:44 -0700, James Kanze wrote:
>On May 27, 1:27 pm, Gennaro Prota <address@spam_this.comwrote:
>On 26 May 2007 17:41:44 -0700, James Kanze wrote:
>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? :-)

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.

--
Gennaro Prota -- C++ Developer, For Hire
https://sourceforge.net/projects/breeze/
May 28 '07 #8

P: n/a

Ok thanks to all for the replies
V.Z.

--
Posted via a free Usenet account from http://www.teranews.com

May 29 '07 #9

This discussion thread is closed

Replies have been disabled for this discussion.