473,405 Members | 2,444 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Noob Question Here...c++

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
Jul 22 '05 #1
9 1527
Eliot Bisky writes:
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.

Jul 22 '05 #2
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...
"osmium" <r1********@comcast.net> wrote in message
news:bv************@ID-179017.news.uni-berlin.de...
Eliot Bisky writes:
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.

Jul 22 '05 #3
Eliot Bisky wrote:
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.

Jul 22 '05 #4
I am greatly confused
-EB

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bv********@dispatch.concentric.net...
Eliot Bisky wrote:
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.

Jul 22 '05 #5
Eliot Bisky wrote:
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.
Jul 22 '05 #6
Eliot Bisky writes:
I am greatly confused
-EB

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bv********@dispatch.concentric.net...
Eliot Bisky wrote:
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.


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.
Jul 22 '05 #7
ahh ic..hmmm i'll see what I can do...

thnx

"lilburne" <li******@godzilla.net> wrote in message
news:bv************@ID-203936.news.uni-berlin.de...
Eliot Bisky wrote:
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.

Jul 22 '05 #8
cool got it working correctly, thanks for all the help...

"lilburne" <li******@godzilla.net> wrote in message
news:bv************@ID-203936.news.uni-berlin.de...
Eliot Bisky wrote:
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.

Jul 22 '05 #9

"Eliot Bisky" <af******@charter.net> wrote in message
news:10*************@corp.supernews.com...
I am greatly confused
-EB

"Gianni Mariani" <gi*******@mariani.ws> wrote in message
news:bv********@dispatch.concentric.net...
Eliot Bisky wrote:
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.

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



Jul 22 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: davestrike | last post by:
I am a noob to sql and asp programming. I am working on a db for the gaming squad I am a member of. One of the pages I created is a roster list of all the squad members. Part of this roster is...
2
by: Nonee | last post by:
Hello- I am writing a program that loads a tv guide listing into a control. I looked at the datagridview control but it doesn't seem to have the ability to overlap columns. Basically, I would...
2
by: Dan McCollick | last post by:
Hi All, Noob question that I can not seem to figure out: I am trying to implement a screenscraper to pull data from two seperate websites, here is some test code so far: public static void...
0
by: AndyW | last post by:
Hey folks. I am trying to get a soap wsdl service working and have a bit of a noob php programming question for it. I'm using PHP 5.x btw. I have written a soap server that contains a...
2
by: Link360 | last post by:
Im a complete noob and im proud of it. I am excited in learning everything about the C++ language. Right now im trying to make tic-tac-toe. Go ahead laugh. here is what i have so far ...
3
by: warhero721 | last post by:
HI. First i like to point out i suck at programing. All i realy whant to know is how do i git my site orginized.I whant to know how to do somthing like this Chatroom fourm sighn up event...
1
by: Japskunk | last post by:
I am having trouble updating a SQL table through the GridView "Auto" Enable Edit Feature... I am connecting to a SQL 2000 Server with a SQLDataSource I have created the Update Query in the Command...
6
by: Lang Murphy | last post by:
I'm baaaaack... some of you answered a question I had last week. Only problem is: I'm a dope who doesn't understand most of what y'all posted. Raw noob when it comes to .Net and C#. So I'm going...
0
by: irtehnoob | last post by:
Hi there, I'm pretty new to c# (started up a few days ago) and I'm making a simple program, so far everything is going good except I can't figure somethine out.. hence why I'm here. Anyways, in my...
4
by: Arch Stanton | last post by:
I'm trying to bind data in a dataset (obtained from an Access DB) to a listbox in ASP.net. I know my dataset is being created properly because it displays fine in a datagrid, but I can't get it to...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.