CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Mar 2004
    Posts
    4

    I've run into a bit of a problem

    I am taking a c++ class and I have a program due, the assignment is to write a program that uses functions with reference parameters that will have the user input the amount of a purchase and the amount tendered, then it will calculate how many 10 dollar bills, 5 dollar bills, 1 dollar bills, quarters, dimes, nickels, pennies are due back, then output it. I've run into a problem because I'm not quite exactly sure how to start it, mainly Im not sure how I would do the math (mainly the math is the part I'm having trouble in). I forgot to add that int main() can only include function calls and variable declarations, all the math and stuff must be done in functions...

    my calcChangedue function is

    void calcChanDue(double amtTen, double purchtotal, double &tendoll, double &fivedoll,double &onedoll, double &quarter, double &dime, double &nickel, double &penny);
    {

    double difference=amtTen-purchtotal;
    //ten dollar bill
    therest=difference%10;
    tendoll=ceil(difference/10);
    //fivedollar bill
    therest=therest%5;
    fivedoll=ceil(therest/5);
    //onedollarbill
    therest=therest%1;
    onedoll=ceil(therest/1);
    //quarters
    therest=therest%.25;
    quarter=ceil(therest/.25);
    //dimes
    therest=therest%.10;
    dime=ceil(therest/.10);
    //nickels
    therest=therest%.05;
    nickel=ceil(therest/.05);
    //pennies
    therest=therest%.01;
    penny=ceil(therest/.01);

    return;
    }
    //end function
    Last edited by chaos9786; March 5th, 2004 at 01:13 AM.

  2. #2
    Join Date
    Feb 2003
    Posts
    377
    I don't think the mod operator (%) works like that with doubles. When you get down to cents, I'd suggest multiplying by 100 and using integers.

  3. #3
    Join Date
    Mar 2004
    Posts
    4

    thanks

    wow, thanks. I should have picked that up...

  4. #4
    Join Date
    Aug 2002
    Posts
    78
    Why even use a float or double to begin with? Fixed-level math can better be done with integers, and then only needs to be converted to decimal in the output.

    Code:
    int amountInCents = 12991;
    int working = amountInCents;
    
    int tens = working / 1000;
    working -= tens * 1000;
    
    int ones = working / 100;
    working -= ones * 100;
    
    int quarters = working / 25;
    working -= quarters * 25;
    
    int dimes = working / 10;
    working -= dimes * 10;
    
    int nickles = working / 5;
    working -= nickles * 5;
    
    int pennies = working;
    
       printf("$%d.%02d is %d tens, %d ones, %d quarters, %d dimes, %d nickles, and %d pennies!\n", amountInCents / 100, amountInCents % 100, tens, ones, quarters, dimes, nickles, pennies);

    OMG, that ran right the first time, too! 12 minutes, including manhandling Visual C++ to create a new project.


    Adding extra denominations (twenties, fives, 50-cent pieces, 2-dollar bills, 3.14-dollar bills, etc.) is left as an exercise.

  5. #5
    Join Date
    Apr 1999
    Posts
    27,449
    Originally posted by Gorgor
    OMG, that ran right the first time, too! 12 minutes, including manhandling Visual C++ to create a new project.
    Funny how those "killer programming assignments" back in college days now take just a few minutes to complete

    The old "convert a number to words" assignment was the classic problem where the students would fret and bite their nails to complete, and some never completed it (given three weeks to finish it). In reality, a good programmer could finish that assignment in half an hour.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 5th, 2004 at 01:48 PM.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured