Thread: updating countdown

  1. #1
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448

    Question updating countdown

    or rather countup. What I want to do is display on the screen a percentage of the work completed. If I just use cout it will write one percentage after the other. But what I want to do is something like what you see when you're formatting your Hard Drive that shows in the same place.
    I've got a workaround for this but It's crap, limits a lot my possibilities and uses 100% of the resources.
    Any ideas?
    TIA
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  2. #2
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Tried something simple like gotoxy() then writing the percent?

  3. #3
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Clearing the screen inbetween cout's would do it as well.

  4. #4
    Magically delicious LuckY's Avatar
    Join Date
    Oct 2001
    Posts
    856
    Actually, just repeatedly clearing the screen wouldn't be so great. Either you would have to redraw all the text that should be displayed on the screen or you would have nothing on the screen except the percent (most likely not an option). Aside from that, you may want the percent somewhere on the screen aside from (0, 0), such as on the bottom line or whatever.

  5. #5
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>Any ideas?
    This might work for you :-)
    Code:
    #include <iostream>
    #include <iomanip>
    
    // Assume Windows or POSIX
    #if defined(__WIN32__)
      #include <windows.h>
      #define sleep Sleep
    #else
      #include <unistd.h>
      #define sleep usleep
    #endif
    
    using namespace std;
    
    int main()
    {
      for (int i = 0; i <= 100; i++)
      {
        cout<<'\r'<< setw(3) << i <<'%'<<flush;
        sleep(100);
      }
    }
    *Cela*

  6. #6
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    Thanks a lot Cela, not sure why it works though doesn't seem like it should, maybe that carriage return...
    Anyhow, it works great!
    >>clearing the screen between cout's would do it as well
    That's what I had but it uses most of the resources. On one program I was clearing the screen and re-writing everything in an infinite loop. Man, that took resources. 100% throughout.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  7. #7
    Registered User Cela's Avatar
    Join Date
    Jan 2003
    Posts
    362
    >>not sure why it works though doesn't seem like it should, maybe that carriage return...
    Yep, it's the carriage return. The cout statement returns to the beginning of the line and prints 4 characters, then the next call returns to the start and again prints 4 characters. So it looks like the counter is updating :-)
    *Cela*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. constant updating
    By Wick in forum C++ Programming
    Replies: 4
    Last Post: 08-18-2003, 09:00 PM
  2. Replies: 4
    Last Post: 08-13-2003, 07:25 PM
  3. updating database
    By datainjector in forum C# Programming
    Replies: 2
    Last Post: 07-11-2003, 01:01 AM
  4. file processing updating record error
    By uuser in forum C Programming
    Replies: 2
    Last Post: 04-27-2003, 12:13 AM
  5. delay()?/sleep()?/need for countdown/
    By Robin Hood in forum C++ Programming
    Replies: 2
    Last Post: 07-19-2002, 04:04 PM