Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > cout at a particulart line

Reply
Thread Tools

cout at a particulart line

 
 
Toro
Guest
Posts: n/a
 
      01-18-2004
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




 
Reply With Quote
 
 
 
 
Aggro
Guest
Posts: n/a
 
      01-18-2004
Toro wrote:

> 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 <iostream>

> 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++.
 
Reply With Quote
 
 
 
 
Jacques Labuschagne
Guest
Posts: n/a
 
      01-18-2004
Aggro wrote:
> int main()
>
>> cout<<"Hello World"<<endl;
>> cout<<" " <<endl;

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


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

 
Reply With Quote
 
=?ISO-8859-1?Q?Le_G=E9ant_Vert?=
Guest
Posts: n/a
 
      01-18-2004
Aggro wrote:

> Toro wrote:
>
>> 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 <iostream>
>
>> 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++.


ain't that \n instead of \r ?
\n = new line
\r = return to the beginning of current line
na ?
 
Reply With Quote
 
Toro
Guest
Posts: n/a
 
      01-18-2004
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" )



 
Reply With Quote
 
Christoph Rabel
Guest
Posts: n/a
 
      01-18-2004
Toro wrote:
> 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
 
Reply With Quote
 
Kevin Goodsell
Guest
Posts: n/a
 
      01-18-2004
Toro wrote:

> 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 <iostream> instead.

> void main() {


main returns int in C++. Always has, always will.

> cout<<"Hello World"<<endl;
> cout<<" " <<endl;
> }
>


-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      01-18-2004
Jacques Labuschagne wrote:
> Aggro wrote:
>
>> int main()
>>
>>> cout<<"Hello World"<<endl;
>>> cout<<" " <<endl;

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

>
>
> 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

 
Reply With Quote
 
Thomas Matthews
Guest
Posts: n/a
 
      01-18-2004
Le Géant Vert wrote:
> Aggro wrote:
>
>> Toro wrote:
>>
>>> 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 <iostream>
>>
>>> 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++.

>
>
> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: cout vs std::cout Hendrik Schober C++ 7 10-07-2008 12:02 AM
Re: cout vs std::cout Stefan Ram C++ 7 09-30-2008 01:55 PM
can cout<<""; can cout<<""; contribute ? wangzhihuii@yahoo.com.cn C++ 4 09-15-2005 06:16 PM
std::cout vs cout Pmb C++ 2 06-02-2004 03:27 PM
man cout or info cout abi C++ 2 06-28-2003 06:42 PM



Advertisments