C Board  

Go Back   C Board > General Programming Boards > C Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 08-08-2009, 03:06 PM   #1
Registered User
 
Join Date: Aug 2009
Posts: 2
printf refresh screen

How can I make the following code actually update the output as it executes. My problem is, it waits till the end then updates the whole thing at once. (I'm running Freespire using gcc).

Code:
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#define COUNT 20000000

int main()
{
	int x;
	char ak[2];
	char pb[] = "                   ";  //this is a long blank string
	
	system("clear");
	printf("Calculating some immensly important calculation.\n");
	//      |= = = = = = = = = =|  progress bar will look like this
	printf("|                   | 0% complete.");
	for (x=1;x<11;x++)
	{
		delay();
                pb[(x-1)*2]='=';
		printf("\r");    //return to beginning of line
		printf("|%s| %d%% complete.",pb,x*10);
	}
	printf("\n\nPress Enter to exit.");
	gets(ak);
	
	system("clear");
	
	return (0);
}

delay()   //this function is simply a delay.
{
	int tv;
	double zxc;
	for(tv=0;tv<COUNT;tv++)
		zxc=sqrt(tv+5);
}
And no, this doesn't serve any useful purpose. I'm simply playing around with C in an attempt to learn it.
nelsonhoover is offline   Reply With Quote
Old 08-08-2009, 03:10 PM   #2
Registered User
 
Join Date: Sep 2004
Location: California
Posts: 2,845
You can try using fflush(stdout) to ensure that the output buffer is flushed to the screen.
__________________
bit∙hub [bit-huhb] n. A source and destination for information.
bithub is offline   Reply With Quote
Old 08-08-2009, 03:11 PM   #3
subminimalist
 
MK27's Avatar
 
Join Date: Jul 2008
Location: NYC
Posts: 3,944
Use fflush(stdout):
Code:
printf("hello world\n"); fflush(stdout);
__________________

Accuracy and integrity mean nothing if you don't make it past the censors...PYTHAGORAS
MK27 is offline   Reply With Quote
Old 08-08-2009, 06:13 PM   #4
Registered User
 
Join Date: Aug 2009
Posts: 2
It worked

Thanks, that did the trick quite nicely. Thanks for your help.
nelsonhoover is offline   Reply With Quote
Reply

Tags
printf, refresh screen

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Problems reading entered race times C loopymoo26 C Programming 12 05-23-2009 07:38 AM
My calculation, delete and update functions dont work (Programming language C) TKaeWoods C Programming 2 03-05-2009 10:25 AM
get keyboard and mouse events ratte Linux Programming 10 11-17-2007 05:42 PM
I need help on this particular Linked List problem sangken C Programming 11 08-06-2006 12:26 AM
whats wrong with this? petedee C Programming 32 01-06-2004 10:28 PM


All times are GMT -6. The time now is 05:03 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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