Results 1 to 7 of 7

Thread: c++ function

  1. #1
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    19

    Unanswered: 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!

  2. #2
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    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

  3. #3
    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 Attached Files

  4. #4
    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.

  5. #5
    Join Date
    Feb 2003
    Location
    Ontario, Canada
    Posts
    19
    No, why do you ask?

  6. #6
    Join Date
    Jul 2003
    Location
    SoCal
    Posts
    721
    Just curious
    That which does not kill me postpones the inevitable.

  7. #7
    Join Date
    Feb 2004
    Location
    In front of the computer
    Posts
    15,579
    Provided Answers: 54
    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

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •