Solved

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

Posted on 2008-10-09
9
323 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
LVL 45

Expert Comment

by:Kdo
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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Preface I don't like visual development tools that are supposed to write a program for me. Even if it is Xcode and I can use Interface Builder. Yes, it is a perfect tool and has helped me a lot, mainly, in the beginning, when my programs were small…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use while-loops in the C programming language.

774 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