Percentage

This is a discussion on Percentage within the C Programming forums, part of the General Programming Boards category; I have been working on a program that calculates the minimum potential of a number of particles, the amount of ...

  1. #1
    Registered User
    Join Date
    Jun 2003
    Posts
    28

    Post Percentage

    I have been working on a program that calculates the minimum potential of a number of particles, the amount of time the program runs can be specifed by the user. Since the user can have the program to run for a couple of hours, what I need is to have the program to print out the percentage that the program has completed (printing out the percentage on one line and it keeps refreshing, just like when uncompressing big files under DOS there is a percentage bar showing the amount has completed).

    Here is what I have right now:

    percent = esc_time / TIME_TO_RUN * 100;

    esc_time is the amount of time the program has been running
    TIME_TO_RUN is the time request by the user to run

    and I need to print out the variable "percent" and having it keeps refreshing. Appreciated if anyone can tell me how to do!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Want the simple way to do it? The standard DOS console tends to be 25x80 (rows x columns). As such, just print aproximately 23 newlines, followed by the line of text you want to "refresh". Depending on how your "prompt" is displayed, you may have to use 24 newlines. Typically you'll find that using the full 25 causes an odd wrapping effect.

    However, that being said, since your prompt (a single line) would always end up on the bottom line, any number of newlines greater than 23 should work for what you need.

    Otherwise you're looking at non-standard functions. (IE: DOS's conio.h, Borland's 'gotoxy' functions, the curses library, etc.)

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jun 2003
    Posts
    28
    Sorry, I'm not quite understanding, can you show me a sample code, I think it's much easier to understand from codes. Thanks

  4. #4
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    I think I read the question a lot differently from Quzah.

    If you want to be able to print status messages, then you'll probably need to either place a print function in your main function (I'm assuming it does some sort of loop), or to create another thread to occasionally print a new update while the primary thread just goes happily about its business.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  5. #5
    Registered User
    Join Date
    Jun 2003
    Posts
    28
    yes, it's a status message, are there any functions to do that?

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Code:
    void status( const char * message )
    {
        printf(
            "\n\n\n\n\n\n\n\n\n\n\n\n"
            "\n\n\n\n\n\n\n\n\n\n\n\n"
            "%s", message );
        fflush( stdout );
    }
    
    int main( void )
    {
        while( !done_doing_stuff )
        {
            do_stuff( );
    
            if( time( NULL ) % 100 == 0 )
            {
                prepare_message( message_to_send );
                status( message_to_send );
            }
        }
        return 0;
    }
    You could use threads, but why?

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    This is ugly, but maybe something along this line would work.
    Code:
    #include <stdio.h>
    #include <time.h>
    
    int main(void)
    {
       double duration;
       time_t end, now, start = time(NULL), then = now = start;
       struct tm *stop = localtime(&start);
       stop->tm_hour += 1; /* one hour from now */
       end = mktime(stop);
       duration = end - start;
       for ( ; now < end; now = time(NULL) )
       {
          if ( now > then )
          {
             double progress  = now - start;
             double remaining = duration - progress;
             double percent   = 100.0 * remaining / duration;
             printf("\ruptime: %.0f seconds, %5.2f%% remaining", progress, percent);
             fflush(stdout);
             then = now;
          }
          /*
           * Do real work here.
           */
       }
       return 0;
    }
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  8. #8
    Been here, done that.
    Join Date
    May 2003
    Posts
    1,161
    I was about to post the same answer as Dave:

    printf("\ruptime: %.0f seconds, %5.2f%% remaining", progress, percent);
    fflush(stdout);
    The '\r' returns to the beginning of the current line. Note there is no '\n' at the end.

    The fflush() statement may or may not be needed based on your system. I've never needed it.
    Definition: Politics -- Latin, from
    poly meaning many and
    tics meaning blood sucking parasites
    -- Tom Smothers

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Percentage Complete?
    By Abda92 in forum C++ Programming
    Replies: 16
    Last Post: 09-21-2007, 03:04 PM
  2. Matching string by a percentage?
    By Hexxx in forum C++ Programming
    Replies: 3
    Last Post: 06-21-2007, 08:45 PM
  3. Calculating a percentage
    By swgh in forum C++ Programming
    Replies: 3
    Last Post: 11-24-2006, 09:24 AM
  4. Getting a percentage from a bunch of random numbers?
    By kidkash in forum C Programming
    Replies: 6
    Last Post: 02-11-2003, 07:42 AM
  5. please help me fix my coding for this assignment
    By sayeem81 in forum C Programming
    Replies: 1
    Last Post: 01-29-2002, 01:45 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21