sprintf, makefiles, function problems?

Status
Not open for further replies.

MongoMania

Ars Legatus Legionis
12,478
Okay, I'm having a major major problem it seems with a bit of code that as far as I can see, shouldn't have any problems. I'm compiling on an AIX machine, haven't tried any other *nices.

Here's the deal: had to split a program into 2 parts to practice with make files (no problems there, I don't think). Basically, in the original code, there was this:

sprintf(sales_rec.tax, "%9f", 1.08125);
sprintf(sales_rec.tax_payable, "%20.2f", (atof(monthly_sales_rec.sell_price)*atof(sales_rec.tax)));

where sales_rec.tax and .tax_payable are defined in a struct, blah blah, that works *perfect*. Now, that's the part I had to move over into a second part for the make file. so, in the ubiquitously named part2.c, we have:

float getTax()
{
float tax_rate;
tax_rate=1.08175;
return tax_rate;

}

float calcTaxPayable(float monthly_sell, float tax)
{
printf("Monthly sell is %f", monthly_sell);
printf(" Tax is %f \n", tax);

return monthly_sell*tax;

}

getTax was defined that way because return 1.08175 was getting the same result.

the new code in the main() looks like:
sprintf(sales_rec.tax, "%9.5f", getTax());
sprintf(sales_rec.tax_payable, "%20.2f", calcTaxPayable(atof(monthly_sales_rec.sell_price), atof(sales_rec.tax)));

What does this give me? Garbage. 0.0000000 for getTax(), sometimes -nan. Here's what I've also done to try and narrow down the problem to no avail:

(in main)
float tax;
tax = getTax();
printf("%f\n", tax);
(0.000000000 or -nan) is result
printf("%f\n", getTax());
ditto

putting printf("%f\n", tax_rate) in getTax() before return yields
1.08175, like it's supposed to.

Also, everything in the struct is char. .tax is char tax[10], and .tax_payable is char tax[21].

Can anyone give me any clues as to what's going on here?

Thanks in advance.
 
Status
Not open for further replies.