Thread: need some quick help

  1. #1
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66

    need some quick help

    I'm at yet another exercise in my book:
    Code:
    Create a program 
    that prompts the user to enter an amount of money between $0 
    and $10(decimal places aloud). Determine how many quarters(25c)
     dimes(10c) nickels(5c) and pennies(1c) are needed to make up that
     amount. Output this information to the screen and ensure the 
    output makes gramatical sense.
    I'm not that far into the program - but I've hit a bit of a snag already. Here is my code:
    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        double cash = 0;
        const double quarter = .25;
        const double dime = .10;
        const double nickel = .05;
        const double penny = .01;
        double quarters = 0;
        double dimes = 0;
        double nickels = 0;
        double pennies = 0;
        double change = 0;
        
        cout <<"Please enter an amount of money from $0 dollars, to $10 dollars: ";
        cin >> cash;
        cout << endl; 
        
        if((cash >= 0) && (cash <= 10)){
                 quarters = cash / quarter;
                 change = quarters * quarter;
                 cash = cash - change;
                 
                 cout << quarters;
                 cout << endl;
                 cout << cash;
                 cout << endl;
                 
                 }
        else{
             cout <<"You entered an invalid amount of money: ";
             cout << cash;
             cout << endl;
             }
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    The problem lies somewhere with the variable 'cash' of type double. When I output it to the screen after the program's done calculating how much money is left to work with after the number of quarters has been found - the value of change is printed to the screen as '0'. However, the number of quarters comes out fine. I'm very confused!

    Here is the program's logic - just so we all know what's supposed to happen - the user enters an amount of money... then the program finds out how many quarters there are in the amount - the value of all the quarters is added up and stored as 'change'.. which is then subracted from cash to come up with the amount of money minus the quarters... which is 'cash'.

    I'll take that an apply it with dimes, nickels, and pennies as well - but that's after we all figure out why cash is displayed as 0! My guess is that I made a stupid mistake with the arithmatic that I fail to see.
    Oh my goodness.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    quarters = cash / quarter;
    change = quarters * quarter;
    cash = cash - change;
    Let's examine this if cash is, say, $1.20.
    Code:
    quarters = cash / quarter;    quarters = 1.2 / .25 = 4.8
    change = quarters * quarter;    change = 4.8 * .25 = 1.2
    cash = cash - change;    cash = 1.2 - 1.2 = 0
    You do have a slight problem in your algorithm. Try this code:
    Code:
    quarters = static_cast<int>(cash / quarter);
    Which would be
    Code:
    quarters = cash / quarter;    quarters = 1.2 / .25 = 4
    change = quarters * quarter;    change = 4 * .25 = 1
    cash = cash - change;    cash = 1.2 - 1 = .2
    That should fix it.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Modulos are another nice way to do it:
    Code:
    quarters = (cash - (cash % 0.25) / 0.25)
    change = cash % 0.25
    Wait, you only want whole numbers of quarters right?

  4. #4
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    Modulo operator only works for ints, so it won't work since floats
    have to be used:

    Create a program
    that prompts the user to enter an amount of money between $0
    and $10(decimal places aloud).
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Modulo operator only works for ints, so it won't work since floats
    You can use fmod() (in <cmath>), which is like a modulus for doubles. But my suggestion should fix the problem.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  6. #6
    Registered User
    Join Date
    Feb 2006
    Posts
    54
    Hmm, thats a strange flaw.

  7. #7
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Quote Originally Posted by dwks
    Code:
    quarters = cash / quarter;
    change = quarters * quarter;
    cash = cash - change;
    Let's examine this if cash is, say, $1.20.
    Code:
    quarters = cash / quarter;    quarters = 1.2 / .25 = 4.8
    change = quarters * quarter;    change = 4.8 * .25 = 1.2
    cash = cash - change;    cash = 1.2 - 1.2 = 0
    You do have a slight problem in your algorithm. Try this code:
    Code:
    quarters = static_cast<int>(cash / quarter);
    Which would be
    Code:
    quarters = cash / quarter;    quarters = 1.2 / .25 = 4
    change = quarters * quarter;    change = 4 * .25 = 1
    cash = cash - change;    cash = 1.2 - 1 = .2
    That should fix it.
    I see the problem now! thatnks for pointing it out! I never thought once about a remainder.. but if I did - I know for sure that I would have known to use a static cast!
    Oh my goodness.

  8. #8
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    hmm, now having updated the code to handle dimes, nickels and pennies -- I am getting some funky output in the penny category.. have a look:

    Code:
    #include <cstdlib>
    #include <iostream>
    
    using namespace std;
    
    int main()
    {
        double cash = 0;
        const double quarter = .25;
        const double dime = .10;
        const double nickel = .05;
        const double penny = .01;
        double quarters = 0;
        double dimes = 0;
        double nickels = 0;
        double pennies = 0;
        double change = 0;
        
        cout <<"Please enter an amount of money from $0 dollars, to $10 dollars: ";
        cin >> cash;
        cout << endl; 
        
        if((cash >= 0) && (cash <= 10)){
                 quarters = static_cast<int>(cash / quarter); //kills a remainder
                 change = quarters * quarter;
                 cash = cash - change;
                 
                 dimes = static_cast<int>(cash / dime);
                 change = dimes * dime;
                 cash = cash - change;
                 
                 nickels = static_cast<int>(cash / nickel);
                 change = nickels * dime;
                 cash = cash - change;
                 
                 pennies = static_cast<int>(cash / penny);
                 change = pennies * penny;
                 cash = cash - change;
                 
                 cout << quarters <<" quarters\n";
                 cout << dimes <<" dimes\n";
                 cout << nickels <<" nickels\n";
                 cout << pennies <<" pennies\n";
                 
                 }
        else{
             cout <<"You entered an invalid amount of money: ";
             cout << cash;
             cout << endl;
             }
        
        system("PAUSE");
        return EXIT_SUCCESS;
    }
    I sometimes can end up with a negative amount of pennies, and sometimes when there should be a penny - there is no penny... For example: in the program if I enter 1.20 I get 4 quarters(1.00) one dime (.10) and 1 nickel(.05) for a combined total of $1.15. If I enter .05, I get one nickel and -5 pennies... ect.

    Obvously I'm not accounting for some change in the values in the aglorithm -- could someone spot the problem? My brain is in the process of turning to mush!
    Oh my goodness.

  9. #9
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    well first since you can't have negitive money you might as well use a unsigned. Second, when ever you are getting odd ammounts when you are using #s or even strings, just start cout'ing them before and after they should change, then you can see where you program is messing them up

  10. #10
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66
    Is there another algorithm I should be using to solve this problem? One that is a bit more manageable?
    Oh my goodness.

  11. #11
    C++ beginner
    Join Date
    Jun 2004
    Posts
    66

    more loop trouble

    nothing -- ignore
    Last edited by mabufo; 02-20-2006 at 04:55 PM.
    Oh my goodness.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. Do you know...
    By davejigsaw in forum C++ Programming
    Replies: 1
    Last Post: 05-10-2005, 10:33 AM
  3. Questions on basic Quick Sort
    By Weng in forum C++ Programming
    Replies: 4
    Last Post: 12-16-2003, 10:06 AM
  4. Quick Sort Help
    By NavyBlue in forum C Programming
    Replies: 1
    Last Post: 03-02-2003, 10:34 PM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM