ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Hi. For a programming class I am in,I have to make a change program. It seems to work fine,except that if I put in 8.55 as a bill and 10 as payment,it does not give back 2 dimes. I was wondering if someone would mind looking at the code at telling me what is wrong. Thanks.
Code:
#include<iostream>
#include<cmath>
using namespace std;
int main()
{
double bill,payed,change,penny,twenty,ten,five,quarter,dime,nickel,dollar;
cout <<" 2\\1\\07\nChange\n\n"
<<"Welcome to the Checkout\n";
cout <<"Please put in your total bill:";
cin >>bill;
cout <<"\nHow much will you pay with?:";
cin >>payed;
while(payed<bill)
{
cout <<"\nYou need more:";
cin>>payed;
}
change=(payed-bill);
cout.precision(2);
cout.setf(ios::fixed);
cout <<"\nYour Bill:";
cout <<"\nChange:$" <<change <<"\n";
twenty=(int(change/20));
change=(change-(twenty*20));
ten=(int(change/10));
change=(change-(ten*10));
five=(int(change/5));
change=(change-(five*5));
dollar=(int(change/1));
change=(change-(dollar*1));
quarter=(int(change/.25));
change=(change-(quarter*.25));
dime=(int(change/.10));
change=(change-(dime*.10));
nickel=(int(change/.05));
change=(change-(nickel*.05));
penny=(change/.01);
cout.precision(0);
cout <<"Twenties:" <<twenty <<"\n"
<<"Tens:" <<ten <<"\n"
<<"Fives:" <<five <<"\n"
<<"Dollar Bills:" <<dollar <<"\n"
<<"Quarters:" <<quarter <<"\n"
<<"Dimes:" <<dime <<"\n"
<<"Nickels:" <<nickel <<"\n"
<<"Pennies:" <<penny <<"\n";
cout <<"Thanks!\n";
return 0;
}
Are you using floating point for this? Never use floating point for monetary calculations. The reason is that if you're using binary floating point (which you almost certainly are), then 0.1 cannot be represented exactly, for roughly the same reason that 1/3 can't be represented exactly in a decimal number system.
Make your variables be long or long long, not float or double. And make a penny be 1, a dollar 100, and so on. That way you won't have a problem with repeating fractional "digits".
That may not be your only problem, but it is a problem with your code.
Hope this helps.
Last edited by wjevans_7d1@yahoo.co; 02-08-2007 at 09:42 PM.
Hi. I understand what you are saying about a decimal number not being able to be represented exactly,but I'm not sure how I should rework this program. I tried making the values for the number of each bill/coin long and making a dollar 100,etc.,but it didn't work. I don't quite understand how the conversions would work since the value for the bill and change needs to be a decimal since it is a dollar amount.
You could define a currency class which stores the value as an integer (or long, or long long), but provides an << operator which puts the . in the correct place. You'd need to override all the arithmetic operators too.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.