Front Office Football Central  

Go Back   Front Office Football Central > Archives > FOFC Archive
Register FAQ Members List Calendar Mark Forums Read Statistics

Reply
 
Thread Tools
Old 02-04-2004, 09:12 PM   #1
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
OT: More Elementary C++ Madness

Purpose: Determine least amount of coins and bills for change.
Specific problem: value returned.

~~~
double change, money, bill;
int hundred; //includes more variables but hundred will suffice for this example

change = money - bill; //user inputs both values in the form 123.45
change = change * 100; //to convert everything to pennies

value = int(change);
~~~

Using the example of a bill of $65.87 and money of $100.00, yields:

change = 34.13
change = 3413
value = 3412 !!!!!!!!!!!!!!!!!!

hundred = value/10000; //returns integer equating to #of bill required
value = value - (10000 * hundred); //calculates remaining change

Can anyone explain why the cast is causing this to happen, and more importantly, how I can prevent it? When you get down to the dimes, nickels and pennies, it throws everything off.

Karim is offline   Reply With Quote
Old 02-04-2004, 10:09 PM   #2
sabotai
General Manager
 
Join Date: Oct 2000
Location: The Satellite of Love
That one's a head scratcher. When I do 65.80, it comes out right. I have no idea why it sends it down to 3412. Weird. I tried a couple of things, but nothing worked.
sabotai is offline   Reply With Quote
Old 02-04-2004, 10:35 PM   #3
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
Yeah. It took me a long time to realize that the cast expression was the problem because my instructor specifically told me to use it to convert from float to int. I was using cout statements checking everything BUT value = int(change).

Somewhere along the way I'm supposed to use modulus division to carry the remainder but that's pretty pointless if the initial data type conversion doesn't work.

I don't think I'll complain about anyone's text sims anymore.
Karim is offline   Reply With Quote
Old 02-05-2004, 01:55 AM   #4
Mr. Wednesday
Pro Starter
 
Join Date: Jul 2003
Location: South Bend, IN
Your problem is the same one I noted in the other post about floating point math -- you have to be careful about rounding. 34.13 happens not to be exactly representable in your computer (when multiplied by 100, it's stored as 3412.9999999999995 [from my watch window]). And casting doesn't round, it truncates. If you want to round, you have to do so explicitly -- try adding 0.5 to change. You'll still run into corner cases, but not for the stuff that your interested in right now.


On a pedantic note, if you're doing C++, you should (strictly speaking) be using static_cast rather than (unsafe) C-style casts.

(OK, I'm being a little pedantic, I still use C-style casts for arithmatic stuff, but I never use them any other time.)
Mr. Wednesday is offline   Reply With Quote
Old 02-05-2004, 01:57 AM   #5
Mr. Wednesday
Pro Starter
 
Join Date: Jul 2003
Location: South Bend, IN
Here's an example program that exhibits the correct behavior:
Code:
#include <iostream> #include <iomanip> int main(void) { double change = 0.0, money = 100.00, bill = 65.87; int hundred; change = money - bill; //user inputs both values in the form 123.45 change = change * 100; //to convert everything to pennies hundred = int(change + 0.5); std::cout << "value = " << hundred << std::endl; char c = 0; std::cin >> c; }
(to be really pedantic, I think I also need to include istream and ostream, but I'm not going to worry about that)

Edit: It tried to parse the includes as HTML. Doh!

Last edited by Mr. Wednesday : 02-05-2004 at 01:58 AM.
Mr. Wednesday is offline   Reply With Quote
Old 02-05-2004, 07:50 AM   #6
Fido
High School Varsity
 
Join Date: Aug 2002
Location: New Hampshire, USA
Or...
Code:
#include #include using namespace std; int main(void) { double money = 100.00; double bill = 65.87; double change = (money - bill) * 100.0; int hundred = int (floor(change + 0.5)); change = double ((hundred % 100) / 100.0); hundred = hundred/100; cout << "Bill Total: $" << bill << "\n" << "Amount Tendered: $" << money << "\n" << "Change Due: $" << hundred << "." << change << endl; char c = 0; cout << "Hit a key to exit" << endl; cin >> c; }
__________________
Author of FOF Reporter and TCY Helper.
Fido is offline   Reply With Quote
Old 02-06-2004, 08:14 AM   #7
Karim
College Starter
 
Join Date: Oct 2000
Location: Calgary
Thanks, guys. It's starting to make sense. Representation at the bit level is something I have to keep in mind. From now on I think I'll do a bunch of cout statements early to see what values are actually being generated and then go from there.

Last edited by Karim : 02-06-2004 at 08:15 AM.
Karim is offline   Reply With Quote
Reply


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools

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

vB code is On
Smilies are On
[IMG] code is On
HTML code is On
Forum Jump


All times are GMT -5. The time now is 11:28 PM.



Powered by vBulletin Version 3.6.0
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.