Solved

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

Posted on 2008-10-09
9
317 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Oop.

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


Kent
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 45

Expert Comment

by:sunnycoder
Comment Utility
No issues :)

0
 

Author Comment

by:Zhongcai
Comment Utility
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
Comment Utility
Does Math.Round(a,b) work with a C compiler?
0
 
LVL 53

Expert Comment

by:Infinity08
Comment Utility
>> 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
Comment Utility
Math.Round works in C#, or managed c++. I misread the question and though he was using C#.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 recursion in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.

762 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

7 Experts available now in Live!

Get 1:1 Help Now