954,498 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

C help calculate grand total

hi guys,

i am stuck... we have to show wats in a file.... then at the end show the grand total of the Profits .. heres the report func....

void report()
{
	struct Prods ssg;
	
	double prof;
	
	FILE *f;
	f = fopen("data.dat","rb");

		printf("\n\n\n==============================================================================\n");
		printf("Prod#   Type   Description	    Qty      Cost        Price        Profit\n");
		printf("==============================================================================\n");

	while(fread(&ssg,sizeof(ssg),1,f))
	{

		printf("\n%-9d%-7d%-21.18s%-2d%13.2lf%13.2lf%13.2lf\n", 
						ssg.prodnum, ssg.prodt, ssg.desc, ssg.prodquan, ssg.prodcost, ssg.prodprice, profits(ssg.prodprice, ssg.prodcost, ssg.prodquan));

	
	}
		printf("\n==============================================================================\n");

		//print grand total of PROFIT

		printf("%d",prof);

	fclose(f);

}


<< moderator edit: added [code][/code] tags >>


thx...

dejaz
Newbie Poster
3 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

How does that function not work? There are a few iffy parts, like not checking fopen for success and not considering that fread can return a non-zero failure code, but there are no obvious errors that I can see this early in the morning. ;)

Dogtree
Posting Whiz in Training
233 posts since May 2005
Reputation Points: 35
Solved Threads: 3
 

thx Dogtree for the reply.... i'll fix the probs u mentioned... func i mentioned works and displays everything .... but the main concern is that i have to add all the profits and at the end show the grand total of the profits... and i dont know how to do that....

thx...

dejaz
Newbie Poster
3 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

I'd imagine something like...

double prof = 0;
// ...
while(/*...*/)
{
   double p = profits(ssg.prodprice, ssg.prodcost, ssg.prodquan);
   // ...
   prof += p;
}
Dave Sinkula
long time no c
Team Colleague
5,058 posts since Apr 2004
Reputation Points: 2,780
Solved Threads: 314
 

I'd imagine something like...

double prof = 0;
// ...
while(/*...*/)
{
   double p = profits(ssg.prodprice, ssg.prodcost, ssg.prodquan);
   // ...
   prof += p;
}


THANKS man... what u said worked perfectly.... ;)

dejaz
Newbie Poster
3 posts since May 2005
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You