Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

C++ HELP! Convert double to int???

Original Message
Name: soriold
Date: February 28, 2005 at 20:30:46 Pacific
Subject: C++ HELP! Convert double to int???
OS: XP
CPU/Ram: 1800 / 1GB
Comment:
Hi:

I am a newbie trying to learn C++. Our instructor gave us several assignments already and I am struggeling with the last one. Here are the instructions:

Write a C++ program that calculates exact change. In order to receive full credit, please remember that only int arithmetic is exact, so you'll need to break up your double into two ints, the one before the decimal point and the one after the decimal point.

Enter the sales price: 5.66
Enter the cash paid: 10.75
Here's your change: 5.09
5 dollars, 1 nickel and 4 pennies
Want to try again? [y/n] n


I definitely don't expect anybody to write the program for me but I need help with regards to the point where he wants us to convert the input of type DOUBLE to 2 INTEGERS... so let's say $5.66 will be INT 5 and INT 66, then I do the same thing with the 10.75... BUT HOW CAN I CALCULATE THE DIFFERENCE??? Do I have to put them back together? I am so confused and I need it by tomorrow morning to get any credit for it!!!

I appreciate any help you guys can give me...

Cheers,

Stefan


Report Offensive Message For Removal


Response Number 1
Name: ChanSahib
Date: March 1, 2005 at 05:38:21 Pacific
Subject: C++ HELP! Convert double to int???
Reply: (edit)
Okay to start off with consider the following
(as a guide)

to get the dollar value use the int function
if SalesPrice is a double
DollarSP = int (SalesPrice)
CentSP = (SalesPrice*100)-DollarSP

(or you could multiply the input values by 100 and then convert to ints - , and display your results differently *starts to go off on a tangent* .....)



Report Offensive Follow Up For Removal

Response Number 2
Name: Don Arnett
Date: March 1, 2005 at 14:38:19 Pacific
Subject: C++ HELP! Convert double to int???
Reply: (edit)
In your example it's easy:

10.75 - 5.66 = 5.09 becomes

75 - 66 = 9
10 - 5 = 5 becomes 5.09


Where you have to worry is this example:

10.66 - 5.75 - 4.91

66 - 75 = ??

You'll have to 'borrow' 100 from the dollar side so that you'll have

166 - 75 = 91
9 - 5 = 4

If the teacher doesn't require that you use your method, it seems that it would be easier to multiple the dollar amounts by 100, then convert to int, do the arithmetic and then convert back..

10.75 - 5.75 = 5.09

mult by 100

1075.00 and 575.00

convert to int

1075 and 575

subtract 1075 - 575 = 509

convert to double

5.09

Be sure to come back and let us know if our suggestions helped!


Report Offensive Follow Up For Removal

Response Number 3
Name: soriold
Date: March 2, 2005 at 23:37:53 Pacific
Subject: C++ HELP! Convert double to int???
Reply: (edit)
Thanks a lot for all your help... I eventually figured it out myself and used an if-statement for the borrowing part...

Thanks a lot!

Stefan


Report Offensive Follow Up For Removal




Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: C++ HELP!  Convert double to int???

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




CPU and Graphics Upgrade Questions

VIRUS ALERT in Taskbar, HELP!

DSHUB24 Connection Problems

need help with dsl and dial up

novel 3.12


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC