Noob Question Here...c++


E

Eliot Bisky

Hello all,

I have a real n00b question. I just started my c++ class about 2
weeks ago and we have an assignment. We have to make a program that makes
change, and then shows how many dollars/quarters/dimes/nickles/pennies in
change the customer gets back....

I could swear my math is correct(I checked it on my calculator over
10 times), I just have no idea why it is not working correctly. It seems
the dollars/quarters/dimes seems to be working but the nickles and pennies
are kinda messed up...When I put an amount of $20.00 owed and $22.72 paid..I
of course get $2.72 in change...and then it displays 2 dollar 2 quarter 2
dime 0 nickle 1 penny.

Take a look at my code in C++:

//Program Purpose : To help students produce change from a sale

#include <iostream>
using namespace std;

int main()
{
float owe = 0.0;
float paid = 0.0;
float change = 0.0;
int dollar = 0 ;
int quarter = 0 ;
int dime = 0 ;
int nickle = 0;
int penny = 0;

//enter input items
cout << "Enter Amount Owed: " ;
cin >> owe;
cout << "Enter Amount Paid: " ;
cin >> paid;

//calculate total owed in change
change = paid - owe;
dollar = change / 1;
quarter = (change - dollar) / .25;
dime = (change - dollar - (quarter * .25)) / .1;
nickle = (change - dollar - (quarter * .25) - (dime * .1)) / .05;
penny = (change - dollar - (quarter * .25) - (dime * .1) - (nickle * .05))
/ .01;

//display output items
cout << "change: " << change << endl;
cout << "dollar(s): " << dollar << endl;
cout << "quarter(s): " << quarter << endl;
cout << "dime(s): " << dime << endl;
cout << "nickel(s): " << nickle << endl;
cout << "penny(s): " << penny << endl;

return 0;
}
//end main function

Thanks for the help!!

-EB
 
Ad

Advertisements

O

osmium

Eliot said:
I have a real n00b question. I just started my c++ class about 2
weeks ago and we have an assignment. We have to make a program that makes
change, and then shows how many dollars/quarters/dimes/nickles/pennies in
change the customer gets back....

I could swear my math is correct(I checked it on my calculator over
10 times), I just have no idea why it is not working correctly. It seems
the dollars/quarters/dimes seems to be working but the nickles and pennies
are kinda messed up...When I put an amount of $20.00 owed and $22.72 paid..I
of course get $2.72 in change...and then it displays 2 dollar 2 quarter 2
dime 0 nickle 1 penny.

Take a look at my code in C++:

file://Program Purpose : To help students produce change from a sale

#include <iostream>
using namespace std;

int main()
{
float owe = 0.0;
<snip>
Float and double are both approximations to an underlying number and can
cause problems when exact comparisons are made. In a problem of this nature
use int or long for the variables.
 
E

Eliot Bisky

I replaced all float w/int and it did not even give me di/nick/penny
amounts..just 2 X dollar when $2.72 change was displayed...
 
G

Gianni Mariani

Eliot said:
I replaced all float w/int and it did not even give me di/nick/penny
amounts..just 2 X dollar when $2.72 change was displayed...

Your units must be in cents to do this.
 
L

lilburne

Eliot said:
I am greatly confused

Floating point arithmetic is not exact and a little bit of
inaccuracy can creep in, especially with division. If you
want to avoid those inaccuracies thenyou need to use integer
arithmetic, but of course for that to work you can't be
working in dollars and cents, you have to work in cents.

Example $2.72 must be treated as 272 cents.
 
Ad

Advertisements

O

osmium

Eliot said:
I am greatly confused
-EB

He means that you can't have .25 in your program any more. This can only be
represented by 0 or 1. It is up to your program to convert .25 dollars to
25 (cents). No decimal point, it is an *integer*. You pretty much have to
discard most of what you posted in your first post.
 
E

Eliot Bisky

ahh ic..hmmm i'll see what I can do...

thnx

lilburne said:
Floating point arithmetic is not exact and a little bit of
inaccuracy can creep in, especially with division. If you
want to avoid those inaccuracies thenyou need to use integer
arithmetic, but of course for that to work you can't be
working in dollars and cents, you have to work in cents.

Example $2.72 must be treated as 272 cents.
 
Ad

Advertisements

D

Dan Moos

Eliot Bisky said:
I am greatly confused
-EB
I'm no pro but:

Here is what they are saying. Your first attempt involved floating point
math, which is of limited precision, so that when you are looking for exact
results, you will sometimes get unexpected answers.

So it was suggested that you use only integers. (ints and longs). Your
problem is that you blindly just changed all your floats to ints. That isn't
quite enough.

Lets walk through your example, but only using ints like you attempted. This
will better demonstrate your issue.

item costs 20.00
you give the person 22.72 (I gotta wonder on what planet this exact exchange
would occur on ;) )

any way, you do your subtraction, and get 2.72 as change.

here is where you lose it so's to speak.

You divide 2.72 by 1. The answer is of course, 2.72 BUT since you made the
dollar variable an int, it gets truncated to 2.
when you made your dollar amount an int, it was truncated down to 22. either
way, at this point everything appears to sill work, because you are working
with the integer part of the amount. Even after conversions turn your floats
into ints, the integer part remains unscathed. The problems start when you
attempt to use the decimal parts.

next you do this

quarter = (change - dollar) / .25;

or quarter = (2.72 - 2)/ .25.

when change was a float, it did it like this.
2.72-2 = .72. .72/.25 = 2.88 .
your quarter variable is an int, so 2.88 gets truncated down to 2. Still the
proper results.

when you changed your change variable to an int, it went like this:
as an int, the change value of 2.72 becomes just 2. (the .72 gets lopped
off) 2-2 = 0. 0/.25 = 0.
And if you follow this line of reasoning , yoiu can see how blindly
converting the floats to ints will make everything after the dollar amount
become 0.

What the last fellow suggested was to make cents your basic unit, rather
than dollars.

in other words. 2.72 dollars would be better represented as 272 cents.

you'd divide by 100 to get dollars, 25 to get quarters, ect. True, the
remaninders in your division still get lopped off, (72/25 is 2.88,
truncated down to 2), but of course, that's exactly what you want.

the most important way I find to test an algorithm is to "run it on paper"
with simple values, before you trust it with more complex stuff where you
wouldn't be able to tell if it was accurate or not.

good luck

moos out
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top