Solved

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

Posted on 2008-10-09
9
320 Views
Last Modified: 2012-05-05
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
Comment
Question by:Zhongcai
  • 3
  • 2
  • 2
  • +2
9 Comments
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22678593
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
 
LVL 45

Expert Comment

by:Kdo
ID: 22678651
Hi Zhonqcai,

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

What compiler/system are you using?


Kent
0
 
LVL 1

Accepted Solution

by:
rgoble earned 50 total points
ID: 22678659
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
 
LVL 45

Expert Comment

by:Kdo
ID: 22678673
Oop.

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


Kent
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 45

Expert Comment

by:sunnycoder
ID: 22678879
No issues :)

0
 

Author Comment

by:Zhongcai
ID: 22678905
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
 
LVL 45

Expert Comment

by:sunnycoder
ID: 22679029
Does Math.Round(a,b) work with a C compiler?
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22679124
>> 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
 
LVL 1

Expert Comment

by:rgoble
ID: 22679401
Math.Round works in C#, or managed c++. I misread the question and though he was using C#.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
This is a short and sweet, but (hopefully) to the point article. There seems to be some fundamental misunderstanding about the function prototype for the "main" function in C and C++, more specifically what type this function should return. I see so…
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to create, access, and change arrays in the C programming language.

896 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now