• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 348
  • Last Modified:

C programming. my printf("%i", total);

when i key in the values

4 2.5
5 9.6
3 8.6
3 1.6
1 7.6
2 9.5
5 0.6
2 4.9

the program gives me  127 instead of 128
what should i add to the code to solve the bug.
#include <stdio.h>
//file used to log the data is named "log.out"//
#define LOG "log.out"
#include <math.h>
 
int main(void)
{
 
//declare variables//
int new_customer, qty;
double amt, total =0;
FILE *log;
 
//open file with write function//
log = fopen(LOG, "w");
 
//start the loop//
printf("New Customer? (1 for yes; 0 for no): ");
scanf("%i",&new_customer);
printf("\n");
 
while(new_customer)
{
//reset total amount to zero//
total = 0.00;
 
//ask cashier to enter quantity and amount//
printf("Enter quantity and amount (-1 -1 to quit): ");
//program assign the values entered to the identifiers//
scanf("%i %lf",&qty, &amt);
 
while (qty > 0)
{
//prints data entered to "log.out"//
fprintf(log, "%i %.2f ", qty, amt);
//calculates the total result in floating point//
total += qty * amt;
//ask cashier to enter quantity and amount//
printf("Enter quantity and amount (-1 -1 to quit): ");
scanf("%i %lf", &qty, &amt);
}
//adds sentinal points to the file "log.out"//
fprintf(log,"-1 -1"); 
//show the cashier the amount to collect from the customer.//
total = (ceil(total * 100))/100;
printf("The total purchase is : $%i", (int)total);
//prints the total amount collected, actual amount and the amount of losses//
//to the file "log.out"//
fprintf(log, "\n%i %.2f %.2f\n\n",(int)total, total, total - (int)total);
 
printf("\n\nNew Customer? (1 for yes; 0 for no): ");
scanf("%i",&new_customer);
printf("\n");
 
} 
system("PAUSE");	
//quits the program//
return 0;
}

Open in new window

0
Zhongcai
Asked:
Zhongcai
  • 3
  • 2
  • 2
  • +2
1 Solution
 
sunnycoderCommented:
Works for me

]$ ./a.out
New Customer? (1 for yes; 0 for no): 1

Enter quantity and amount (-1 -1 to quit): 4 2.5
Enter quantity and amount (-1 -1 to quit): 5 9.6
Enter quantity and amount (-1 -1 to quit): 3 8.6
Enter quantity and amount (-1 -1 to quit): 3 1.6
Enter quantity and amount (-1 -1 to quit): 1 7.6
Enter quantity and amount (-1 -1 to quit): 2 9.5
Enter quantity and amount (-1 -1 to quit): 5 0.6
Enter quantity and amount (-1 -1 to quit): 2 4.9
Enter quantity and amount (-1 -1 to quit): -1 -1
The total purchase is : $128

New Customer? (1 for yes; 0 for no): 0

What platform are you executing on?
0
 
Kent OlsenData Warehouse Architect / DBACommented:
Hi Zhonqcai,

I run the program with the same data and get 128.

What compiler/system are you using?


Kent
0
 
rgobleCommented:
If you are wanting it to round the value of total up before printing it out there is an easier way to do it. Change the line

total = (ceil(total * 100))/100;

to

total = Math.Round( (total+0.5), 0);

The round function rounds to the nearest value, since you are wanting to round up you need to first add 0.5.
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Kent OlsenData Warehouse Architect / DBACommented:
Oop.

Sorry Sunny.  I was testing the code and didn't see your post.....


Kent
0
 
sunnycoderCommented:
No issues :)

0
 
ZhongcaiAuthor Commented:
thanks guys. I pasted my friend's solution to the question. I was wondering if there is anything better than

total = (ceil(total * 100))/100;

and i guess

total = Math.Round( (total+0.5), 0);

works better
0
 
sunnycoderCommented:
Does Math.Round(a,b) work with a C compiler?
0
 
Infinity08Commented:
>> total = Math.Round( (total+0.5), 0);

Math.Round is not part of C. It isn't even possible, since C doesn't have the concept of methods.

If you are really working in C, then the accepted solution is not correct.


Try this way of rounding instead :

        int dtotal = ((int) ceil(total * 100.0)) / 100;
        printf("The total purchase is : $%d\n", dtotal);


or more likely, if the total needs to include the cents (up to 2 digits), then :

        total = ceil(total * 100.0) / 100.0;
        printf("The total purchase is : $%.2f\n", total);
0
 
rgobleCommented:
Math.Round works in C#, or managed c++. I misread the question and though he was using C#.
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now