cout at a particulart line

Discussion in 'C++' started by Toro, Jan 18, 2004.

  1. Toro

    Toro Guest

    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 :)
    Toro, Jan 18, 2004
    #1
    1. Advertising

  2. Toro

    Aggro Guest

    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++.
    Aggro, Jan 18, 2004
    #2
    1. Advertising

  3. 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";
    Jacques Labuschagne, Jan 18, 2004
    #3
  4. 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 ?
    =?ISO-8859-1?Q?Le_G=E9ant_Vert?=, Jan 18, 2004
    #4
  5. Toro

    Toro Guest

    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" )
    Toro, Jan 18, 2004
    #5
  6. 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
    Christoph Rabel, Jan 18, 2004
    #6
  7. 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.
    Kevin Goodsell, Jan 18, 2004
    #7
  8. 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
    Thomas Matthews, Jan 18, 2004
    #8
  9. 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
    Thomas Matthews, Jan 18, 2004
    #9
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. abi

    man cout or info cout

    abi, Jun 27, 2003, in forum: C++
    Replies:
    2
    Views:
    1,637
  2. Pmb

    std::cout vs cout

    Pmb, Jun 2, 2004, in forum: C++
    Replies:
    2
    Views:
    4,394
    Leor Zolman
    Jun 2, 2004
  3. Replies:
    4
    Views:
    309
    Howard
    Sep 15, 2005
  4. Stefan Ram

    Re: cout vs std::cout

    Stefan Ram, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    463
  5. Hendrik Schober

    Re: cout vs std::cout

    Hendrik Schober, Sep 28, 2008, in forum: C++
    Replies:
    7
    Views:
    427
    Jerry Coffin
    Oct 7, 2008
Loading...

Share This Page