GIDForums  

Go Back   GIDForums > Computer Programming Forums > C Programming Language
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Dec-2006, 23:38
JonK JonK is offline
New Member
 
Join Date: Dec 2006
Posts: 3
JonK is on a distinguished road

printf won't display output until my program terminates


My program operates as I would expect, with one iritating exception. I wanted to have a sort of status bar displayed
so that I could see what kind of progress was being made; however, when I execute the program the status bar isn't displayed,
that is until the program terminates, at which point it prints the whole thing at once.
CPP / C++ / C Code:

	/*-----------------------------------------------\
	|                                                |
	|  This program's intention is to store numbers  |
	|  on a user-defined interval in files named for |
	|  the number of prime factors they have.  Each  |
        |  number will be paired with its Phi-Stat.      |
	\_______________________________________________*/

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

int main()
{

/* Variable Declarations */

  FILE *fp;
  int working, starting_point, end_point, number, oldnumber, counter, outer, euler, ruler, barcounter;
  char filename[25];
  float interval;
  div_t temp;

/* Lower and Upper Bound Definitions */

  printf("Please enter a starting point: ");
  scanf("%d", &starting_point);
  printf("Please enter an end point: ");
  scanf("%d", &end_point);
  interval = (end_point - starting_point) / 10;

/* Status bar initialization */

  printf("<");
  ruler=(interval + 0.5);
  barcounter = 0;

/* Beginning Outer Loop Checking Each Number */

  for(outer = starting_point; outer <= end_point; outer++)
  { 
    working = outer;
    counter = 0;
    euler = outer;
    barcounter++;

/* Beginning Inner Loop Determining Factorability of Each Number */

    for(number = 2; number <= outer/2; number++)
    {
    temp = div(working, number);
    if (temp.rem == 0)
      {
      counter++; 
      working = working/number;
      if (oldnumber != number)
        {
        oldnumber = number;
        euler = (euler * (number - 1)) / (number);
        }
      number--;
      }
    }
  if (outer == working)
    {
    euler = outer-1;
    }

/* Deciding on a file name in which to store the resultant values */

  sprintf(filename, "factors_%d", counter);
  fp=fopen(filename, "a");
  fprintf(fp, "%9d%9d\n", outer, euler);
  fclose(fp);

/* This part, which is supposed to be a bar displaying the current state of completion of the program, 
Is behaving strangely.  It displays what I expected it to display, but it shows up only after execution 
has terminated, rendering it useless */

  if (barcounter == ruler)
    {
    printf("=%d", barcounter);
    barcounter = 0;
    }
  }
  printf("=>\n");
}

  #2  
Old 02-Dec-2006, 02:10
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: printf won't display output until my program terminates


Every time you output the status bar info, add fflush(stdout); and see what happens. You may have to flush the output buffer to get the display properly.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #3  
Old 02-Dec-2006, 07:24
JonK JonK is offline
New Member
 
Join Date: Dec 2006
Posts: 3
JonK is on a distinguished road

Re: printf won't display output until my program terminates


Thank you so much WaltP, your suggestion worked perfectly.

Cheers, JonK
  #4  
Old 02-Dec-2006, 14:32
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: printf won't display output until my program terminates


Ahh, good. Output in some compilers is buffered, so it waits until the buffer is full before displaying the informatin. fflush() tells the program to output the stuff now, don't wait.
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
  #5  
Old 02-Dec-2006, 18:14
JonK JonK is offline
New Member
 
Join Date: Dec 2006
Posts: 3
JonK is on a distinguished road

Re: printf won't display output until my program terminates


That seems counterintuitive to me, although I haven't the faintest notion of how a computer works, or why languages are constructed the way they are. I suppose if I want to continue using C, I should take the time to learn a little about both. Anyhow, your suggestion was a big help to me; I'd likely not have discovered that little tidbit for days if I had to figure it out on my own.

Thanks
 
 

Recent GIDBlogAccepted for Ph.D. program by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
creating a file in [c] i hate c C Programming Language 15 21-Nov-2005 13:52
My program can run,but warning were display on Vc++ fwongmc C Programming Language 5 08-Dec-2004 11:15
RSA program fwongmc C Programming Language 11 08-Nov-2004 23:15
problem with the program under Borland awmp-jansen C Programming Language 3 01-Jul-2004 18:05
using vector or array oshiotse C++ Forum 4 16-Apr-2004 11:59

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 18:48.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.