[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

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

Posted on 2008-10-09
9
Medium Priority
?
340 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 46

Expert Comment

by:Kent Olsen
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 200 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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 46

Expert Comment

by:Kent Olsen
ID: 22678673
Oop.

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


Kent
0
 
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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
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 create, access, and change arrays in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

650 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