Problem with change return problem.

This is a discussion on Problem with change return problem. within the C++ Programming forums, part of the General Programming Boards category; I am writing a program that is supposed to take an amount of change the user imputs and then the ...

  1. #1
    Registered User
    Join Date
    Dec 2008
    Posts
    35

    Question Problem with change return problem.

    I am writing a program that is supposed to take an amount of change the user imputs and then the program is supposed to output the number of quarters, dimes, nickles, and pennies needed to make up the amount given prior.

    I get the program to run and able to input the change. When the output of coins is given you get the right number of qurters multiplied by 100. Ex. If you put in 75 for change you will get 300 for quarters, 3000 for dimes, etc, etc. The first digit is correct but the rest isn't.

    It doesn't make sense to me. Are my equations written wrong or did I go about this the wrong way.

    Code:
    #include <iostream>
    
    int change;
    int quarter;
    int dime;
    int nickle;
    int pennie;
    
    int main()
    {
    	std::cout << "Amount of change: " ;
    	std::cin >> change ;
    
    	quarter = ( change / .25 ) ;
    	dime = ( change / .25 ) / .1 ;
    	nickle = ( ( change / .25 ) / .1 ) / .05 ;
    	pennie = ( ( ( change / .25 ) / .1 ) / .5 ) / .01 ;
    
    	std::cout << "Anount due:" << '\n' ;
    	std::cout << "   quarters: " << quarter ;
    	std::cout << "   dimes: " << dime ;
    	std::cout << "   nickles: " << nickle ; 
    	std::cout << "   pennies: " << pennie ;
    
    	return (0);
    }
    Thank you in advance and helping me work through this.

  2. #2
    Registered User
    Join Date
    Apr 2006
    Posts
    2,012
    There are two major problems.
    1) The logic for quarters is correct, but then what you do for dimes and down is wrong. The number of dimes is not the number of quarters divided by 0.1 as your logic suggests.

    2)Your logic seems oblivious to the types of variables. You apparently ask for a real(non-integer) number as the amount of change, but store it as an int. Then you try to divide it by a double, which represents a real number in C++. The results of this is of type double, but sometimes you store it as an int, other times you use it in a formula with doubles. This results in a number of errors.

    Before you rush to change types to double, you should be aware that 0.01, 0.1, and 0.05 cannot be exactly represented, and using it may result in rounding errors, particularly if you convert the result to to an int or if you do long calculations with it and other. A solution is to immediately multiply the input by 100, cast to int, and use the int value for future calculations.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  3. #3
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    Thank you for posting. I dont exactly follow what you are talking about but I think I have a general understanding. I will try and change my code up tonight and post the new one in and see if it is any better.

  4. #4
    Registered User
    Join Date
    Nov 2009
    Posts
    82
    Do the math of your code on paper, it doesn't work.

    Code:
    quarter = ( change / .25 ) ;
    dime = ( change / .25 ) / .1 ;
    nickle = ( ( change / .25 ) / .1 ) / .05 ;
    pennie = ( ( ( change / .25 ) / .1 ) / .5 ) / .01 ;
    Change = 100.

    No:
    Quarters: Change / 25 = 4 (exception...)
    Dimes: Change / 25 / 10 = 0.4
    Nickels: Change / 25 / 10 / 5 = 0.08
    Pennies: Change / 25 / 10 / 5 / 1 = 0.08

    Yes:
    Quarters: Change / 25 = 4
    Dimes: Change / 10 = 10
    Nickels: Change / 5 = 20
    Pennies: Change / 1 = 100


    The other thing is, you wouldn't give back all of the quarters, dimes, nickels and pennies that can fit in to Change. So you have to figure out what the best way to give change is, which can be a simple algorithm using MOD and what not, which is for you to figure out / attempt. >:]
    Last edited by since; 12-03-2009 at 02:03 PM.

  5. #5
    Registered User
    Join Date
    Dec 2008
    Posts
    35
    I am not sure if this is what you guys were suggeting me to do but I got this to run with no errors and ouput the correct numbers.

    Here is the code I have now to date. Any inprovements for me?

    Code:
    #include <iostream>
    
    int change;
    int quarter;
    int dime;
    int nickle;
    int pennie;
    
    int main()
    {
    	std::cout << "Amount of change: " ;
    	std::cin >> change ;
    
    	quarter = ( change / 25 ) ;
    	dime = ( change % 25 ) / 10 ;
    	nickle = ( ( change % 25 ) % 10 ) / 5 ;
    	pennie = ( ( ( change % 25 ) % 10 ) % 5 ) / 1 ;
    
    	std::cout << "Anount due:" << '\n' ;
    	std::cout << "   quarters: " << quarter ;
    	std::cout << "   dimes: " << dime ;
    	std::cout << "   nickles: " << nickle ; 
    	std::cout << "   pennies: " << pennie << '\n' ;
    
    	return (0);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. can some one please tell me the cause of the error ?
    By broli86 in forum C Programming
    Replies: 8
    Last Post: 06-26-2008, 08:36 PM
  2. Hybrid ADT Problem
    By cpp_is_fun in forum C++ Programming
    Replies: 1
    Last Post: 11-18-2005, 08:40 AM
  3. very weird problem (pointers I think)
    By hannibar in forum C Programming
    Replies: 2
    Last Post: 10-11-2005, 06:45 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. Certain functions
    By Lurker in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2003, 12:26 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21