Money Counter Program

This is a discussion on Money Counter Program within the C++ Programming forums, part of the General Programming Boards category; Hey, My homework assignment for my C++ class was to write a program that can find the minimum amount of ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155

    Money Counter Program

    Hey,

    My homework assignment for my C++ class was to write a program that can find the minimum amount of coins necessary for change for a purchase.

    Here's the code I came up with:
    Code:
    #include <iostream>
    #include <string>
    
    using namespace std;
    
    void calculate ( double change, int * coins );
    void getValues ( double &change );
    
    int main ( int argc, char * argv[] )
    {
    	string coinTypes[6] = { "Dollars", "Half-Dollars", "Quarters", "Dimes", "Nickels", "Pennies" };
    	int coinValues[6] = { 0, 0, 0, 0, 0, 0 };
    	double change = 0.0;
    
    	getValues ( change );
    	calculate ( change, coinValues );
    
    	for ( int i = 0; i < 6; i++ )
    	{
    		cout << coinTypes[i] << " = " << coinValues[i] << endl;
    	}
    
    	system("pause");
    	return 0;
    }
    
    //
    //Function to get the values from the user
    //
    void getValues ( double &change )
    {
    	double total, paid;
    
    	cout << "Enter total cost: ";
    	cin >> total;
    	cout << "Enter amount paid: ";
    	cin >> paid;
    
    	change = static_cast<double>(paid - total);
    }
    
    //
    //function to calculate how many coins are needed
    //
    void calculate ( double change, int * coins )
    {
    	double div[] = { 1, .5, .25, .1, .05, .01 };
    	double result;
    
    	//Figire out the number of coins needed;
    	for ( int i = 0; i < 6; i++ )
    	{
    		result = static_cast<double>( change / div[i] ); //see how many of the coin value go into the change amount
    		change -= static_cast<double>( (int)result * div[i] ); //subtract the value of the coin * the amount of it from the change
    		coins[i] = static_cast<int>( result ); //take the number of coins and store in the necessary variable
    	}
    }

    Here's the program's output
    Code:
    Enter total cost: 56.78
    Enter amount paid: 100
    Dollars = 43
    Half-Dollars = 0
    Quarters = 0
    Dimes = 2
    Nickels = 0
    Pennies = 1
    Press any key to continue . . .
    Everything is good except there should be 2 pennies, not 1...

    I think there's some sort of precision error or something in the numbers.

    Any help would be greatly appreciated.

    Thanks,

    Matt N
    ~guitarist809~

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    If I add the amount owed to the change, I get 99.99 which is fairly close to 100. I think this inaccuracy has something to do with 0.(9) = 1. In other words, the result it gave you is as close to 100 as can be represented by floating point.

    So if you want to be exact with the till all the time, calculate in pennies using whole number types. You would still be dividing by denominations like you have in the original algorithm so it isn't that different.

  3. #3
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Code:
    result = static_cast<double>( change / div[i] )
    since div[i] and change are already double variables.

    In fact, the only cast you need in the whole program is this one:
    Code:
    coins[i] = static_cast<int>( result )

    Edit: Of course, this is quite pointless, as the right solution is to count money in an integer value as pennies.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  4. #4
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    void calculate ( double change, int * coins );
    void getValues ( double &change );
    Did your teacher show you to do it like that (the text in red)?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    Quote Originally Posted by Elysia View Post
    Did your teacher show you to do it like that (the text in red)?
    No, but my teacher likes us using pass-by-reference variables (the double& part). I did the int * part (extra credit for using pointers/arrays, since we just started doing basic functions). I know int * coins is a pretty cheesy way of doing this (asking for some sort of a buffer overflow error) but for this program it isn't too dangerous.

    I'm just confused why i don't get 2 pennies instead of 1... =\
    ~guitarist809~

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    I mean the way you place it.
    double &var is what you wrote, not double& var or double & var.
    And then you write int * var, not int* var or int *var.
    Two different styles, even.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    I dunno.

    I've always used the & sign directly next to a variable like double &abc, or if it's just a declaration, i do double&. For pointers it would be like double * var, or for a declaration double*.

    Is that bad or something?
    ~guitarist809~

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Well, no.
    But every C++ programmer chooses a style based upon his or her preferences.
    Either the * or & binds to the name (typical C) or to the type (typical C++) or in the middle.
    I just want you to be aware that there are 3 different styles and that you should choose one for yourself (not pick up one because your teacher said so).
    Some people say it's good to be consistent and in some situations and jobs, you will have to be, so it might also be a good idea to stick to one style.
    I like to put it next to the type to emphasis that it's part of the type (because int* var and float* var aren't the same type - something that int *var and float *var doesn't give the impression of).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    Elysia, stop derailing threads with your irrelevant opinions about style, please.

    As I've stated before, the change plus the amount owed comes to 99.99, a precision error on the part of the computer. I recommend reading about rounding errors and 0.(9) being equal to 1, both of which may be playing a role here in your program. Rounding errors occur because the range of a floating point must be expressed in a finite number of bits, so approximations of numbers like 100 or 0.2 or 0.1 will appear, to the detriment of your calculations. Most real-world financial software uses whole number math, in small denominations like pennies, to represent currency at some stage for these reasons.

    So, you have my suggestion -- use integer math to get exact change every time. If you cannot do that (assignment restrictions?) then I recommend a comment stating what you have learned about the problem and your teacher will likely give you credit.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by citizen View Post
    Elysia, stop derailing threads with your irrelevant opinions about style, please.
    At the very least, I feel it is important that everyone knows about style and do not blindly follow what the teacher uses or says.
    Besides, an answer to the problem was given, no?
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    >> do not blindly follow what the teacher uses or says.

    What in blue blazes gave you that impression?

  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Just checking.
    Many people just blindly do that. I've witnessed it before.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  13. #13
    Registered User
    Join Date
    Mar 2006
    Location
    USA::Colorado
    Posts
    155
    So... if I take the input as a string, find the '.' character and split everything from the left to an int called dollars, and the right to another int called change, it would work better? Since I'm only doing integer division...

    I never fully understood what the answer was in the first place. How can you store a number like 56.78 in an int...
    ~guitarist809~

  14. #14
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,667
    >> Just checking.

    No I mean, really, what gave you that impression? You just started talking about it, and his code didn't have readability problems. You just assumed his teacher told him how to write it like that and reacted.

    >> How can you store a number like 56.78 in an int
    Convert the dollar amount into cents by multiplying 100. The result can then be truncated to a long.
    Last edited by whiteflags; 09-26-2008 at 02:18 AM.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,548
    Quote Originally Posted by citizen View Post
    No I mean, really, what gave you that impression? You just started talking about it, and his code didn't have readability problems. You just assumed his teacher told him how to write it like that and reacted.
    Because
    1) OP was using double &var
    2) OP was using int * var

    Two different styles, plus using the 1st, which according to some, should be uncommon in C++.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. I pay (money) for a program
    By Mszx in forum Projects and Job Recruitment
    Replies: 6
    Last Post: 01-08-2007, 12:22 PM
  2. Help: Program to calc space and money for apartments
    By MightyM991 in forum C Programming
    Replies: 4
    Last Post: 09-22-2005, 09:11 AM
  3. Replies: 2
    Last Post: 10-23-2004, 03:46 AM
  4. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM

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