Dec 14, 2011

C/C ++: Progress indicator


 today I've taken to find out: victory:. Then I show you the code and Briefly Explain It does not and Have a lot of mystery That Once You put on it.

/ / ================================================ ================
/ /
/ / Filename: percentage.cpp
/ /
/ / Description:
/ /
/ / Version: 1.0
/ / Created: 10/11/09 11:51:42
/ / Revision: none
/ / Compiler: g + +
/ /
/ / ================================================ ================

# Include <iostream>
# Include <cstdlib>
# Include <ctime>

using namespace std;

/ / ============================================ === FUNCTION ========
/ / Name: main
/ / Description:
/ / ================================================ ===============
int main ()
{
   try
   {
      unsigned int Operations = 500;
      float inc = 100.0f / ( static_cast < float > (operations) -1.0);
      float progress = 0.0f;
      for (unsigned int i = 0; i <Operations; i + +)
      {
         usleep (10000);
         progress + = inc;
         court << "Progress:" << static_cast <unsigned int > (progress)
                 << "% \ r" <<flush;
      }
   court <<endl;
   }
   catch (exception & ex)
   {
      court <<ex.what () <<endl;
   }
   return EXIT_SUCCESS;
}
The most important thing we can find it on line 39. \ r is an escape character that allows us to position the cursor at the beginning of the line, while flush allows us to synchronize the buffer associated with a stream . Christian, that shows us what we want to show without having to enter a carriage return. The rest is just running so you can see an example. In line 36 we find the function usleep sleep that allows the process X microseconds, while the calculation in line 32 increases the variable progress in each operation.

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Premium Wordpress Themes