If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > Delphi, C etc > c++ function

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-21-04, 11:02
bbk bbk is offline
Registered User
 
Join Date: Feb 2003
Location: Ontario, Canada
Posts: 19
c++ function

Hi,
I have to implement the following function:

void SetAmount(double dAmount);

I have to extract dollars and cents from dAmount.
So far I have the following:

int x = dAmount * 1000;
int r = x % 10;
if(r >= 5) x += 10 - r;
else x -= r;
Cents = (x % 1000) / 10;

I'm stuck on how to extract the dollars.

Thanks in advance!
Reply With Quote
  #2 (permalink)  
Old 06-21-04, 13:13
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Which C++ compiler are you using?

How much have you covered in class, specifically have you covered the std namespace and the packages within it? Can you post the assignment as given, there seem to be a lot of missing details.

-PatP
Reply With Quote
  #3 (permalink)  
Old 06-23-04, 08:33
bbk bbk is offline
Registered User
 
Join Date: Feb 2003
Location: Ontario, Canada
Posts: 19
I am doing this is phobos. I have cover std namespace and I've included my assignment specs. If there is anything else you need just let me know.

Thanks.
Attached Files
File Type: doc OOP244_Assignment1.doc (42.5 KB, 332 views)
Reply With Quote
  #4 (permalink)  
Old 06-23-04, 11:36
Seppuku Seppuku is offline
Useless...
 
Join Date: Jul 2003
Location: SoCal
Posts: 721
Is the professor for your class Maya Strelnikova by chance?
__________________
That which does not kill me postpones the inevitable.
Reply With Quote
  #5 (permalink)  
Old 06-23-04, 13:46
bbk bbk is offline
Registered User
 
Join Date: Feb 2003
Location: Ontario, Canada
Posts: 19
No, why do you ask?
Reply With Quote
  #6 (permalink)  
Old 06-23-04, 13:51
Seppuku Seppuku is offline
Useless...
 
Join Date: Jul 2003
Location: SoCal
Posts: 721
Just curious
__________________
That which does not kill me postpones the inevitable.
Reply With Quote
  #7 (permalink)  
Old 06-23-04, 15:08
Pat Phelan Pat Phelan is offline
Resident Curmudgeon
 
Join Date: Feb 2004
Location: In front of the computer
Posts: 12,605
Extending the logic you posted, I'd use:
Code:
x = 1000 * dAmount;

Cents = x / 10 % 100		// assign hundredths as int
+ (x % 10 < 5) ? 0 : 1;		// adjustment for rounding

Dollars = x / 1000;		// first cut at Dollars

if (99 < Cents) {		// if overflow
   Cents -= 100;
   Dollars++;
   };
-PatP
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On