Results 1 to 4 of 4

Thread: Iostream cout on loop same line

  1. #1
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Iostream cout on loop same line

    How i can invoke cout to print on the same place .....?

    same as linux comman curl -O http ******

    and display 20% on console on the same place not new line.... or append on line...

    Qt Code:
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. int main ()
    5. {
    6. int n;
    7. cout << "Enter the starting number > ";
    8. cin >> n;
    9.  
    10. while (n>0) {
    11. cout << "%" << n << endl;
    12. --n;
    13. }
    14.  
    15. cout << "FIRE!";
    16. return 0;
    17. }
    To copy to clipboard, switch view to plain text mode 

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

    Default Re: Iostream cout on loop same line

    Use "\r" (carriage return) to return to the beginning of the same line (without the line feed). Remember you have to flush stdout to see anything if you don't use "\n".

  3. The following user says thank you to wysota for this useful post:

    patrik08 (7th December 2006)

  4. #3
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Iostream cout on loop same line

    Quote Originally Posted by wysota View Post
    Use "\r" (carriage return) to return to the beginning of the same line (without the line feed). Remember you have to flush stdout to see anything if you don't use "\n".
    so is corect ?

    if the nummer is moore as 1200 i can see .... otherwise go to fast .....

    only color i can not print......

    on php i print \033[01;31m to console to display red on c++ console is not ansi?


    Qt Code:
    1. #include <iostream>
    2. #include <stdio.h>
    3.  
    4. using namespace std;
    5.  
    6. int main ()
    7. {
    8. int n;
    9. cout << "Enter the starting number > ";
    10. cin >> n;
    11.  
    12. while (n>0) {
    13. cout << "%" << n << "\r";
    14. fflush ( stdin );
    15. --n;
    16. }
    17.  
    18. cout << "FIRE!";
    19. return 0;
    20. }
    To copy to clipboard, switch view to plain text mode 

  5. #4
    Join Date
    Aug 2006
    Location
    Switzerland
    Posts
    52
    Thanked 13 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Iostream cout on loop same line

    You can use ANSI codes in c++, too:
    Qt Code:
    1. #include <iostream>
    2. using namespace std;
    3.  
    4. extern "C" {
    5. #include <unistd.h>
    6. }
    7.  
    8. int main() {
    9. int n = 0;
    10. cout << " %";
    11. while (n < 10) {
    12. // move cursor to first column, print number, move cursor to fifth column
    13. cout << "\033[1G" << n*10 << "\033[5G" << flush;
    14. sleep(1);
    15. n++;
    16. }
    17. // print in red
    18. cout << "\nTest color: \033[1;31mred\033[0m" << endl;
    19. }
    To copy to clipboard, switch view to plain text mode 
    The Wheel weaves as the Wheel wills.

  6. The following user says thank you to danadam for this useful post:

    patrik08 (8th December 2006)

Similar Threads

  1. Find and line endings
    By mikeh in forum Qt Programming
    Replies: 1
    Last Post: 22nd October 2006, 10:16
  2. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 18:42
  3. Workload in a QThread blocks main application's event loop ?
    By 0xBulbizarre in forum Qt Programming
    Replies: 14
    Last Post: 9th April 2006, 21:55
  4. QListWidget-problem
    By Sarma in forum Qt Programming
    Replies: 7
    Last Post: 7th April 2006, 18:49
  5. While statement inside the main loop
    By OnionRingOfDoom in forum Qt Programming
    Replies: 4
    Last Post: 8th February 2006, 18:17

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.