This is going a little further than I should, but there's really not that much here besides what I've highlighted.
Code:
#include <stdio.h>
#include <time.h>
/**
* Display a progress bar.
* @param value the current value
* @param maxval the maximum value
*/
void gauge(int value, int maxval)
{
static const int scale = 50; /* adjust per your preference */
double progress = (double)value / maxval;
int i, current = progress * scale;
/*
* Print bar |-----+-------|
*/
putchar('|');
for ( i = 1; i < current; ++i )
{
putchar('-');
}
putchar('+');
for ( ; i < scale - 1; ++i )
{
putchar('-');
}
putchar('|');
/*
* Print percentage.
*/
printf(" %g%%", progress * 100.0);
}
int main()
{
int i, top = 25;
for ( i = 0; i < top; ++i )
{
time_t now = time(NULL);
putchar('\r'); /* may work, may not */
gauge(i, top);
while ( time(NULL) == now ); /* ugly delay */
}
return 0;
}
And 0% and 100% don't work the way you might expect. (I gotta leave something.)