Change Calculator

Project for school. I need help solving an issue.

Page 1 of 1

11 Replies - 8552 Views - Last Post: 07 October 2010 - 10:43 PM Rate Topic: -----

#1 Forwardechelon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-October 10

Change Calculator

Posted 07 October 2010 - 08:51 PM

Hi I'm new to this forum and new to programming. I have an assignment from class where I have to write a program that an employee/cashier can enter the total sales amount and the amount given from the customer. The program should calculate and display the change due back to the customer, how many dollars, quarters, dimes, nickels, and pennies to give back. I have the program written and it works with some amounts, but with other amounts it doesn't calcuate the right amout of pennies.

Heres the code:
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

int main ()
{
	//enter variables
	double moneyOwed = 0.0;
	double moneyPaid = 0.0;
	double change = 0.0;
	int change1 = 0;
	int dollars = 0;
	int quarters = 0;
	int dimes = 0;
	int nickels = 0;
	int pennies = 0;

	//enter input
	cout<<"Enter money owed: $";
	cin>>moneyOwed;
	cout<<"Enter money paid: $";
	cin>>moneyPaid;
	cout<<endl;

	//calculation
	if (moneyOwed > moneyPaid)
	{
		cout<<"The amount that was given is not enough, please pay the full amount.";
		cout<<endl;
		cout<<endl;
	}
	else
	{
		cout<<fixed<<setprecision(2);
		change = moneyPaid - moneyOwed;
		change1 = change*100;
		dollars = change1/100;
		quarters = change1%100/25;
		dimes = change1%100%25/10;
		nickels = change1%100%25%10/5;
		pennies = change1%100%25%10%5;
		
		//display
		cout<<"Change due: $"<<change<<endl;
		cout<<"Dollars: "<<dollars<<endl;
		cout<<"Quarters: "<<quarters<<endl;
		cout<<"Dimes: "<<dimes<<endl;
		cout<<"Nickles: "<<nickels<<endl;
		cout<<"Pennies: "<<pennies<<endl;
		cout<<endl;
		cout<<endl;
	}

	system("pause");
	return 0;
}


I think the problem might be when I convert 'double change' to 'int change1' cause I've displayed 'change1's value on the screen and it sometimes wouldn't equal 'change*100'.For instance, moneyPaid(30.00) - moneyOwed(28.73), the change would be (1.27), but change1 would be (126). Any help would be great. Thanks.

This post has been edited by Forwardechelon: 07 October 2010 - 09:02 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Change Calculator

#2 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Change Calculator

Posted 07 October 2010 - 09:15 PM

Your correct to assume this is causing your problem, converting from a double to an int will cause loss of precision.
Was This Post Helpful? 0
  • +
  • -

#3 eker676  Icon User is offline

  • Software Engineer
  • member icon

Reputation: 378
  • View blog
  • Posts: 1,833
  • Joined: 18-April 09

Re: Change Calculator

Posted 07 October 2010 - 09:19 PM

Store all monetary values in integers, always. No exceptions.

Rewrite your program to accept a regular amount i.e. 9.99. Multiply that amount by 100 and store it.

Do all calculations with integers only and it will save you much frustration.
Was This Post Helpful? 2
  • +
  • -

#4 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Change Calculator

Posted 07 October 2010 - 09:28 PM

I will add as a note, there is a way way to get around the loss of precision. You can cast the value of change*100 into change1, like so:
change1 = int (change*100);



This converts the double value to an int. Tha value will still round down though. Add .1 to the change*100 portion and the cast will round down to the needed value.

EDIT: If you can fit your assingment requirements to meet what Eker stated above, that is the better option. Otherwise, what I have wrote is an additional option. Really only if your teacher is making you work with doubles.

-alias

This post has been edited by alias120: 07 October 2010 - 09:32 PM

Was This Post Helpful? 1
  • +
  • -

#5 Forwardechelon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-October 10

Re: Change Calculator

Posted 07 October 2010 - 09:45 PM

I don't have to work with doubles, but how would I get the program to accept a double but store it as an integer, such as 12.76 stored as 1276?
Was This Post Helpful? 0
  • +
  • -

#6 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Change Calculator

Posted 07 October 2010 - 09:50 PM

Read above post on using a cast, Example:

change1 = int (change*100 + .1);  //casts the result of change*100 + .1
                                  //as an int into change1.



As Eker said though, if are not required to use doubles, and not strictly working with doubles, writing your program to use ints is better. No worries about loss of precision.

-alias
Was This Post Helpful? 0
  • +
  • -

#7 Forwardechelon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-October 10

Re: Change Calculator

Posted 07 October 2010 - 09:55 PM

View Postalias120, on 07 October 2010 - 08:50 PM, said:

Read above post on using a cast, Example:

change1 = int (change*100 + .1);  //casts the result of change*100 + .1
                                  //as an int into change1.



As Eker said though, if are not required to use doubles, and not strictly working with doubles, writing your program to use ints is better. No worries about loss of precision.

-alias

Yeah using a cast worked, but whats the best way to go about doing what Eker said to do?

This post has been edited by Forwardechelon: 07 October 2010 - 09:55 PM

Was This Post Helpful? 0
  • +
  • -

#8 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Change Calculator

Posted 07 October 2010 - 10:03 PM

One method would be to create a grammar of how dollars and cents will be represented in your program. You need to accept a dollar amount, and period, and cents amount. You can accept multiple inputs in a single statement.

Ex.
cin>> int >> char >> int;



-alias

This post has been edited by alias120: 07 October 2010 - 10:04 PM

Was This Post Helpful? 0
  • +
  • -

#9 Forwardechelon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-October 10

Re: Change Calculator

Posted 07 October 2010 - 10:14 PM

View Postalias120, on 07 October 2010 - 09:03 PM, said:

One method would be to create a grammar of how dollars and cents will be represented in your program. You need to accept a dollar amount, and period, and cents amount. You can accept multiple inputs in a single statement.

Ex.
cin>> int >> char >> int;



-alias


So?
int dollars = 0;
char period = '';
int cents = 0;

cout<<"Please enter the balance due: ";
cin>>dollars>>period>>cents;



would something like that work?

This post has been edited by Forwardechelon: 07 October 2010 - 10:21 PM

Was This Post Helpful? 0
  • +
  • -

#10 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Change Calculator

Posted 07 October 2010 - 10:20 PM

Close, but why would you create a char period array? If you are only excepting one character type as input, a period, it will stay a period throughout the scope of it's use.

Ex.
int dollarsOwed = 0;
int changeOwed = 0;
char period = '';

cout<<"Please enter amount owed: ";
cin>> dollarsOwed >> period >> changeOwed;
if(period != '.')
{
   cout<<"Invalid, please enter amount as 100.00"<<endl;
   //Handle accordingly
}



You can use that as a template for how you want to structure your input. After you have the input portion down, it should be simple to find how many dollars and cents are owed in change.

-alias

EDIT: To include example

This post has been edited by alias120: 07 October 2010 - 10:29 PM

Was This Post Helpful? 0
  • +
  • -

#11 Forwardechelon  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 07-October 10

Re: Change Calculator

Posted 07 October 2010 - 10:35 PM

oh ok. Thanks.

This post has been edited by Forwardechelon: 07 October 2010 - 10:41 PM

Was This Post Helpful? 0
  • +
  • -

#12 alias120  Icon User is offline

  • The Sum over All Paths
  • member icon

Reputation: 122
  • View blog
  • Posts: 700
  • Joined: 02-March 09

Re: Change Calculator

Posted 07 October 2010 - 10:43 PM

There is no reason to create a character array for period, your only accepting input for one character. So you would use
char period = ' ';
//as opposed to
char period[];



Then accept input for dollars, period, and change. That way you are dealing with two ints, dollars and change. After you get input for amount owed and amount paid, you can find the change due (if any). You have similar logic already laid out in your program above, you just have to alter it to make use of int instead of double.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1