cout at a particulart line

T

Toro

Hi,
I want to control the output such that it always print at the same
line, erasing the line just printed (or erasing a particular posistion at a
certain line). Say, for a simple program like this,

#include <StdAfx.h>
#include <iostream.h>
void main() {
cout<<"Hello World"<<endl;
cout<<":) " <<endl;
}

instead for printing

Hello World
:)

it only print

Hello :)
 
A

Aggro

Toro said:
Hi,
I want to control the output such that it always print at the same
line, erasing the line just printed (or erasing a particular posistion at a
certain line). Say, for a simple program like this,

#include <StdAfx.h>

Non-standard header. You can compile your code without it using your
compiler, but how to do it is off topic in this group. ( Don't get too
stuck with this issue, just understand that it is a non-standard header,
and your code will be fine without it also. )
#include <iostream.h>

#include said:
void main() {

int main()
cout<<"Hello World"<<endl;
cout<<":) " <<endl;

std::cout << "Hello World";
std::cout << "\rHello :)" << std::endl;

return 0;

Return 0 is not required by the standard, but your compiler might give
an warning if you don't use it.

I'm not sure will \r work similary on all platforms, but AFAIK that is
the best you can do using standard C++.
 
J

Jacques Labuschagne

Aggro said:
int main()



std::cout << "Hello World";
std::cout << "\rHello :)" << std::endl;

Why not just
cout << "Hello World";
// erase 5 characters
cout << "\b\b\b\b\b";
 
?

=?ISO-8859-1?Q?Le_G=E9ant_Vert?=

Aggro said:
Non-standard header. You can compile your code without it using your
compiler, but how to do it is off topic in this group. ( Don't get too
stuck with this issue, just understand that it is a non-standard
header, and your code will be fine without it also. )






int main()



std::cout << "Hello World";
std::cout << "\rHello :)" << std::endl;

return 0;



Return 0 is not required by the standard, but your compiler might give
an warning if you don't use it.

I'm not sure will \r work similary on all platforms, but AFAIK that is
the best you can do using standard C++.

ain't that \n instead of \r ?
\n = new line
\r = return to the beginning of current line
na ?
 
T

Toro

for

std::cout << "Hello World";
std::cout << "\rHello :)"<< std::endl;

the output,

Hello :)rld

std::cout << "Hello World";
std::cout << "\rHello :)";

the output
Hello :)

I need to include <iostream.h> and <iostream> if I have cout inside the
program.
I do need <stdfax.h> to have the program compiled without error.

cout<<"Hello World";
cout<<"\b\b\b\b\b";
cout<<":)";

the output is ok but I wonder how I can erase the whole line. (or changed
from something like " it is fun " to "it isn't fun" )
 
C

Christoph Rabel

Toro said:
I need to include <iostream.h> and <iostream> if I have cout inside the
program.

No. You only need <iostream>. iostream.h is an old header
with roughly the same content, that shouldnt be used anymore.

cout is now in the namespace std. So you must write

using std::cout;

cout << "Hello World";

or write

std::cout << "Hello World";
I do need <stdfax.h> to have the program compiled without error.

No, you dont *need* this header. It is special header for MS
Compilers that is used for decreasing compile times. You can
compile your code without it.
cout<<"Hello World";
cout<<"\b\b\b\b\b";
cout<<":)";

the output is ok but I wonder how I can erase the whole line. (or changed
from something like " it is fun " to "it isn't fun" )

Why don't you try a different approach?

Store the current line in a string and modify it before output.

std::string current_line = "Hello World";
cout << current_line;

current_line = "Hello :)";

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

Or you could only count the number of characters in your
output and later print "\b" to delete the whole output.

hth

Christoph
 
K

Kevin Goodsell

Toro said:
Hi,
I want to control the output such that it always print at the same
line, erasing the line just printed (or erasing a particular posistion at a
certain line). Say, for a simple program like this,

There is no way to do this portably, using standard C++. One reason for
this is quite simple - you have no way of knowing that the device
std::cout is "attached to" has this capability. It could be a file, for
example, or a socket, or a printer.

If you want to do this, you should look for some method provided by your
implementation and direct any questions about it to a group that
discusses your implementation.
#include <StdAfx.h>

Please don't post non-standard code here. This group is for discussion
of standard C++. In order to help, we often need to be able to copy &
paste your code into our (standard) compilers, and this won't be accepted.
#include <iostream.h>

That's an old pre-standard header. Use said:
void main() {

main returns int in C++. Always has, always will.
cout<<"Hello World"<<endl;
cout<<":) " <<endl;
}

-Kevin
 
T

Thomas Matthews

Jacques said:
Why not just
cout << "Hello World";
// erase 5 characters
cout << "\b\b\b\b\b";

Just remember that not all output devices perform
a destructive backspace when interpreting the '\b'
escape sequence.

The OP has to use some platform specific functionality
in order to fulfill the requirements. For example,
if the output stream is connected to a tape drive
or a Hex LED display, it won't work.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 
T

Thomas Matthews

Le said:
ain't that \n instead of \r ?
\n = new line
\r = return to the beginning of current line
na ?

No. Some platforms will translate the '\r' escape sequence into '\r\n'.
There is no standard process for satisfying the OP's requirements.
Platform specific functions are necessary, and will be discussed in
other newsgroups.

--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.raos.demon.uk/acllc-c++/faq.html
Other sites:
http://www.josuttis.com -- C++ STL Library book
http://www.sgi.com/tech/stl -- Standard Template Library
 

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

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top