LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-08-2007, 07:50 PM   #1
JAKK
Member
 
Registered: Jun 2006
Distribution: Slackware 11
Posts: 92

Rep: Reputation: 15
c++ change program problem


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;

}

Last edited by JAKK; 02-08-2007 at 08:49 PM.
 
Old 02-08-2007, 09:40 PM   #2
wjevans_7d1@yahoo.co
Member
 
Registered: Jun 2006
Location: Mariposa
Distribution: Slackware 9.1
Posts: 938

Rep: Reputation: 31
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.
 
Old 02-08-2007, 11:05 PM   #3
JAKK
Member
 
Registered: Jun 2006
Distribution: Slackware 11
Posts: 92

Original Poster
Rep: Reputation: 15
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.
 
Old 02-08-2007, 11:31 PM   #4
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
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.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I tell and change what user runs what program? flycast Linux - Newbie 2 12-12-2006 10:06 PM
How to change the library used by a program. wangjinyi Fedora 1 11-23-2005 10:31 AM
program to change avi to vcd? dr_zayus69 Linux - Software 4 09-03-2005 09:44 PM
Change prompt values from within a C++ program pshea Linux - Newbie 0 09-16-2003 11:06 PM
Tcl/Tk Program to change Screen Resolution soda Linux - General 2 01-20-2003 06:05 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:23 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration