Results 1 to 5 of 5

Thread: Updating one part of the output (C++ / Cout)

  1. #1
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Updating one part of the output (C++ / Cout)

    I was just wondering how some programs update certain parts of the terminal output.... Like when showing the percentage done. Its hard to explain...

    Like if the output was this:
    Qt Code:
    1. Percentage: [number]
    To copy to clipboard, switch view to plain text mode 
    Where [number] was continuously updated.

    How is that done?

    Thanks

  2. #2
    Join Date
    May 2007
    Posts
    5
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Updating one part of the output (C++ / Cout)

    This code will do what you're asking for, however, I have only tested it in windows. Its behavior may be different under linux/mac.

    Qt Code:
    1. #include <iostream>
    2. using std::iostream;
    3.  
    4. int main()
    5. {
    6. for (int i=0; i < 5; i++)
    7. cout << "Progress: " << i*25 << "%\r";
    8.  
    9. return 0;
    10. }
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Updating one part of the output (C++ / Cout)

    Shouldn't it be:
    Qt Code:
    1. cout << "\rProgress: " << i*25 << "%";
    To copy to clipboard, switch view to plain text mode 
    ?

    I guess the difference is minimal... Oh, and don't forget to flush the output or you'll see nothing.

  4. #4
    Join Date
    Mar 2006
    Location
    The Netherlands
    Posts
    300
    Thanks
    9
    Thanked 29 Times in 29 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Updating one part of the output (C++ / Cout)

    For completions sake:

    Qt Code:
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. int main() {
    5. for (uint i = 0; i <= 100; ++i) {
    6. cout << "\rProgress: " << i << "%" << flush;
    7. for (uint j = 0; j < 9999999; ++j); // pause
    8. }
    9.  
    10. cout << endl;
    11.  
    12. return 0;
    13. }
    To copy to clipboard, switch view to plain text mode 

    \r means carriage return. Without a \n, it just takes the cursor back to the beginning of the current line. flush flushes the buffer.
    "The strength of a civilization is not measured by its ability to wage wars, but rather by its ability to prevent them." - Gene Roddenberry

  5. #5
    Join Date
    Oct 2007
    Location
    USA
    Posts
    27
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Updating one part of the output (C++ / Cout)

    Alright, thanks all.... Its nice to fill in one of those little gaps of knowledge.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.