Hello!
How is everyone? I know this program is somewhat simple, but I am working on learning classes, and I am trying to implement this in my code. I pretty sure my mathematics done in the program are correct, or at least on the write track.

Here's what I am trying to achieve:

Basically, I am trying to write a class (CoinChanger) that determines the number of coins needed to make a given amount of change.
I am aloud to use dollars (or dollar coins), quarters, dimes, nickels, & pennies. So, I will ask the user to input a amount of change they would like to receive back (with two points of decimal precision).
The class has one constructor, and one set function. The set function is passed a float or double, which represents the amount of change for which it must determine the correct coin change. There are two get functions. The first get function uses references to return the number of dollar coins, quarters, dimes, nickels, and pennies for the amount given. The CoinChanger Class returns the least amount of coins required. The second get function returns the total number of change calculations (Transactions) it has preformed since it was constructed and the total amount of change.

Now, my problem is figuring exactly how I am going to do this with the most amount of efficiency. I am stuck on exactly how I am going to count the total amount of transactions (or calculations) made since the object was created, as well as sum the total of money change was given for.

Here are the specified requirements I am given regarding this program:

I am instructed not to use cin/cout/getline statements in the CoinChanger class.

I have to show money using $ and 2 decimal places of accuracy.

Last but not least, I am asked in let the user continue entering in amount of money to get change for until they wish to stop.

I would be greatly obliged if I could be directed in the proper direction for the efficiency of my layout (for the program), if my class is properly set up, and if I implemented everything correctly.

I am still learning and appreciate any help or pointers that may be given to me, and any guidance on how I set things up/solved things.


Thank you very much, :icon_biggrin:


POTTER

-----------------------------------------------------------------------------------------------------------------------------------------

Here is my code:

// CoinChanger Class
FILE: CoinChanger.h

//   CLASS:		CIS 2275 C++ II
// PROGRAM:		QUIZ #3 |  C++ COIN CHANGER (USING COINCHANGER CLASS)
//  E-MAIL:		********@inbox.com
//    FILE:		CoinChanger.h

#include <string>

using namespace std;

#ifndef  _COINCHANGER_H
#define  _COINCHANGER_H

class CoinChanger
{
private:
	// User input of amount change will be made from
	double Amount;
	// Dollar Coin
	int DollarCoin;
	// Quarter Coin
	int QuarterCoin;
	// Dime Coin
	int DimeCoin;
	// Nickel Coin
	int NickelCoin;
	// Penny Coin
	int PennyCoin;
	// Sum of coin change that was calculated
	double TotalChange;
	// Counts the number of change transactions user made
	int Count;
	
	// Calculates the amount of change given back to user
	void Calculate(int DollarCoin, int QuarterCoin, int DimeCoin, 
		           int NickelCoin, int PennyCoin);

public:
	// Default Constructor
	CoinChanger();

	// Sets amount of money user wants change from
	void setCoinChangeAmount(double A);
	
	// Gets smallest denominations of coin change found in Calculate();
	void getCoinChange(int &DollarCoin, int &QuarterCoin, int &DimeCoin, 
		               int &NickelCoin, int &PennyCoin);
	// Gets (Returns) total number of transactions & sum of change calculated
	int getTransactionTotals(double &TotalChange);

};

#endif

-----------------------------------------------------------------------------------------------------------------------------------------

FILE: CoinChanger.cpp

//   CLASS:		CIS 2275 C++ II
// PROGRAM:		QUIZ #3 |  C++ COIN CHANGER (USING COINCHANGER CLASS)
//  E-MAIL:		********@inbox.com
//    FILE:		CoinChanger.cpp

#include <iostream>
#include <string>
#include <iomanip>
#include "CoinChanger.h"

using namespace std;

CoinChanger::CoinChanger()
{
	int Count = 0;
	double Amount = 0.0;
	double TotalChange = 0.0;

	int DollarCoin = 0;
	int QuarterCoin = 0;
	int DimeCoin = 0;
	int NickelCoin = 0;
	int PennyCoin = 0;

	Calculate(DollarCoin, QuarterCoin, DimeCoin, 
			 NickelCoin, PennyCoin);
}

void CoinChanger::setCoinChangeAmount(double A)
{
	this->Amount = A;
	Calculate(DollarCoin, QuarterCoin, DimeCoin, 
			  NickelCoin, PennyCoin);
}

void CoinChanger::Calculate(int DollarCoin, int QuarterCoin, int DimeCoin, 
							int NickelCoin, int PennyCoin)
{	
	
	int LeftOver;

	if ((int)Amount >= DollarCoin)
	{
		DollarCoin = (int)Amount / 1;
		LeftOver = (int)Amount % 1;
	}
	else if (LeftOver >= QuarterCoin)
	{
		QuarterCoin = LeftOver / 25;
		LeftOver = LeftOver % 25;
	}
	else if (LeftOver >= DimeCoin)
	{
		DimeCoin = LeftOver / 10;
		LeftOver = LeftOver % 10;
	}
	else if (LeftOver >= NickelCoin)
	{
		NickelCoin = LeftOver / 05;
		LeftOver = LeftOver % 05;
	}
	else if (LeftOver >= PennyCoin)
	{
		PennyCoin = LeftOver / 01;
		LeftOver = LeftOver % 01;
	}
		
}

void CoinChanger::getCoinChange(int &DollarCoin, int &QuarterCoin, int &DimeCoin, 
								int &NickelCoin, int &PennyCoin)
{
	this->DollarCoin = DollarCoin;
	this->QuarterCoin = QuarterCoin;
	this->DimeCoin = DimeCoin;
	this->NickelCoin = NickelCoin;
	this->PennyCoin = PennyCoin;

}

int CoinChanger::getTransactionTotals(double &TotalChange)
{
	++Count;
	TotalChange = ++Amount;
	
	return Count;
}

-----------------------------------------------------------------------------------------------------------------------------------------

FILE: Driver.cpp

//   CLASS:		CIS 2275 C++ II
// PROGRAM:		QUIZ #3 |  C++ COIN CHANGER (USING COINCHANGER CLASS)
//  E-MAIL:		********@inbox.com
//    FILE:		Driver.cpp

#include <iostream>
#include <iomanip>
#include <string>

#include "Function.h"
#include "CoinChanger.h"

using namespace std;

int main()
{

	// SHOW USER THE OBJECTIVE SCREEN (HEADER INFORMATION....)
	ObjectiveScreen();

	/*------  Declared Variables  ------*/
	string answer;
	int Count;
	double Amount, TotalChange;
	int DollarCoin, QuarterCoin, DimeCoin, NickelCoin, PennyCoin;
	/*------  End of Declaration  ------*/

	// Default Constructor
	CoinChanger CC;

	// START DO-WHILE LOOP
	do {
		cout << "\n NOTE:    All Currency Amounts MUST Entered With TWO DECIMAL "
			 << "\n          PLACES OF PRECISION. [ Example -->  $5.00 ]" << "\n\n";

		cout << "\n Please Enter The US Dollar Amount Of Change You Would Like:  ";
		cin  >> Amount;

		CC.setCoinChangeAmount(Amount);

                cout.setf(ios::fixed);
		cout.precision(2);
		cout << "\n You Have Entered The US Dollar Amount Of: $" << Amount << "\n";

		cout << "\n The Resultant Coin Change Is: \n";
		CC.getCoinChange(DollarCoin, QuarterCoin, DimeCoin, NickelCoin, PennyCoin);
		cout << "\n  DOLLAR COINS:  " << QuarterCoin;
		cout << "\n QUARTER COINS:  " << DimeCoin;
		cout << "\n    DIME COINS:  " << NickelCoin;
		cout << "\n   PENNY COINS:  " << PennyCoin << "\n";

		CC.getTransactionTotals(TotalChange);
		cout << "\n You Have Made A Total Of " << Count << "Transaction(s). \n";
		
		cout.setf(ios::fixed);
		cout.precision(2);
		cout << "\n The Total Change Calculated Is: $" << TotalChange << "\n";

		cout << "\n Calculate More Coin Change? (YES/NO) ";
		cin  >> answer;

	}while (answer == "YES");

	cout << "\n NOW EXITING C++ COIN CHANGER...... GOOD-BYE! \n\n";

	return (0);

}
// END OF THE C++ COIN CHANGER

-----------------------------------------------------------------------------------------------------------------------------------------

// Header file for program information (not really important)
FILE: Functions.h

//   CLASS:		CIS 2275 C++ II
// PROGRAM:		QUIZ #3 |  C++ COIN CHANGER (USING COINCHANGER CLASS)
//  E-MAIL:		********@inbox.com
//    FILE:		Function.h

#include <iostream>

using namespace std;

// Displays the introduction and program description
void ObjectiveScreen();

-----------------------------------------------------------------------------------------------------------------------------------------

// Not really important
FILE: Functions.cpp

//   CLASS:		CIS 2275 C++ II
// PROGRAM:		QUIZ #3 |  C++ COIN CHANGER (USING COINCHANGER CLASS)
//  E-MAIL:		********@inbox.com
//    FILE:		Functions.cpp

#include <iostream>
#include <iomanip>

using namespace std;

void ObjectiveScreen()
{
	// Author, program title, and program objective.
	cout << "\n ----------------------------------------------------   \n";
	cout << "\n   C++ COIN CHANGER (USING COINCHANGER CLASS) "    << " \n"; 
	cout << "\n ----------------------------------------------------   \n";
	cout << setw(15) <<  "\n	    AUTHOR:	 " << setw(15) << "Potter ";
	cout << setw(17) <<  "\n	     CLASS:	 " << setw(5)  << "CIS 2275 ";
	cout << setw(10) <<  "\n	ASSIGNMENT:   " << setw(20) << "QUIZ #3	\n";
	cout <<  "\n ----------------------------------------------------  \n";
	
	cout << "\n	  QUIZ #3       |       THE COINCHANGER CLASS	   \n";

	cout << "\n The CoinChanger program is similar to a vending machine, "
		 << "\n which much be able to give change (coins). This program	 "
		 << "\n will have a class called CoinChanger, which has a        "
		 << "\n constructor, one set function, and two get functions.    "
		 << "\n When the user has entered the desire amount, the program "
		 << "\n will then tell the exact change they get. The program    "
		 << "\n will also let the user know how many times they have     "
		 << "\n asked for change. \n";
	cout << "\n ----------------------------------------------------   \n";

}

-----------------------------------------------------------------------------------------------------------------------------------------

Thank you once again....
Feel free to ask me if you have any questions regarding my code/code layout.


POTTER

Recommended Answers

All 9 Replies

void CoinChanger::getCoinChange(int &DollarCoin, int &QuarterCoin, int &DimeCoin, 
								int &NickelCoin, int &PennyCoin)
{
	this->DollarCoin = DollarCoin;
	this->QuarterCoin = QuarterCoin;
	this->DimeCoin = DimeCoin;
	this->NickelCoin = NickelCoin;
	this->PennyCoin = PennyCoin;

}

This is a set function, not a get function. Either rename it or make it an actual get function.

void CoinChanger::Calculate(int DollarCoin, int QuarterCoin, int DimeCoin, 
							int NickelCoin, int PennyCoin)
{	
	
	int LeftOver;

	if ((int)Amount >= DollarCoin)
	{
		DollarCoin = (int)Amount / 1;
		LeftOver = (int)Amount % 1;
	}
	else if (LeftOver >= QuarterCoin)
	{
		QuarterCoin = LeftOver / 25;
		LeftOver = LeftOver % 25;
	}
	else if (LeftOver >= DimeCoin)
	{
		DimeCoin = LeftOver / 10;
		LeftOver = LeftOver % 10;
	}
	else if (LeftOver >= NickelCoin)
	{
		NickelCoin = LeftOver / 05;
		LeftOver = LeftOver % 05;
	}
	else if (LeftOver >= PennyCoin)
	{
		PennyCoin = LeftOver / 01;
		LeftOver = LeftOver % 01;
	}
		
}

I get really confused about which variable is part of "this" and which isn't. If a parameter is part of "this", why are you passing it? If not, what's the point of the function? You're changing the values of local variables that will immediately go out of scope since they are not passed by reference. I would strongly advise that if you use local variables which are NOT part of the class, but have names which are the same, always use "this" whenever you ARE referring to a class variable.

Beyond that, since you constructed this as if-else if-else if, at most only ONE code block can be executed. Is that what you want?

Put a while loop in the calculate function.... Loop till left over amount has become 0.

So, this right below isn't good. I wasn't quite sure so I guessed. Can you somewhat elaborate a bit, I am still kinda getting the hand of classes. Should I just make the DollarCoin, QuarterCoin, etc references from the beggining? (Inside the class?)
Also, I want to use a get function for "getCoinChange", but I wasn't sure how to go about doing that......

void CoinChanger::getCoinChange(int &DollarCoin, int &QuarterCoin, int &DimeCoin, 
								int &NickelCoin, int &PennyCoin)
{
	this->DollarCoin = DollarCoin;
	this->QuarterCoin = QuarterCoin;
	this->DimeCoin = DimeCoin;
	this->NickelCoin = NickelCoin;
	this->PennyCoin = PennyCoin;

}
void CoinChanger::Calculate(int DollarCoin, int QuarterCoin, int DimeCoin, 
							int NickelCoin, int PennyCoin)
{	
	
	int LeftOver;

	if ((int)Amount >= DollarCoin)
	{
		DollarCoin = (int)Amount / 1;
		LeftOver = (int)Amount % 1;
	}
	else if (LeftOver >= QuarterCoin)
	{
		QuarterCoin = LeftOver / 25;
		LeftOver = LeftOver % 25;
	}
	else if (LeftOver >= DimeCoin)
	{
		DimeCoin = LeftOver / 10;
		LeftOver = LeftOver % 10;
	}
	else if (LeftOver >= NickelCoin)
	{
		NickelCoin = LeftOver / 05;
		LeftOver = LeftOver % 05;
	}
	else if (LeftOver >= PennyCoin)
	{
		PennyCoin = LeftOver / 01;
		LeftOver = LeftOver % 01;
	}
		
}

I just want to clarify..... I shouldn't use the if else statement? I kinda see why. But, is there a better way, or should I just do the math and not the if else?

Beyond that, since you constructed this as if-else if-else if, at most only ONE code block can be executed. Is that what you want?

I wanted to execute the whole thing, but I wasn't sure which way to do it. Perhaps, should I just do the math, or is there a better way?

Put a while loop in the calculate function.... Loop till left over amount has become 0.

Oh ok, that makes a bunch more sense.........

Thanks I will try that and see.

commented: Always nice to see that some people are actually willing to learn something. Good job! +12

You have several different issues.

  1. public versus private
  2. setter versus getter
  3. pass by reference versus pass by value
  4. static versus non-static methods
  5. Groups of if statements
  6. What you need to pass to a class member function
  7. "this" keyword.
  8. Variable naming and scoping (how do you differentate between a class member variable and a local parameter with the same name.

Don't try to tackle them all at once. Your head will spin and you'll get seemingly contradictory answers. A few tips for now while you're learning. Some may disagree, but I think oversimplification is the way to go FOR NOW.

Thus, my rules for beginners.

  1. Make everything public. This removes all need for "get" and "set" functions.
  2. Never name a method or a local variable or a parameter passed to a method the same as a class variable. This removes all confusion and you never have to worry about the keyword "this".
  3. Worry about static variables and methods later. Your change routine is an excellent candidate for a static method.
  4. Never pass a class data member to a class member function. You already have it, so you don't need to pass it.

You'll unlearn all of these later, because every point I just mentioned is oversimplified and/or plain wrong. But it simplifies your life for now.

So your new function is this:

void CoinChanger::Calculate()
{	
	
	int LeftOver;

	if ((int)Amount >= DollarCoin)
	{
		DollarCoin = (int)Amount / 1;
		LeftOver = (int)Amount % 1;
	}
	else if (LeftOver >= QuarterCoin)
	{
		QuarterCoin = LeftOver / 25;
		LeftOver = LeftOver % 25;
	}
	else if (LeftOver >= DimeCoin)
	{
		DimeCoin = LeftOver / 10;
		LeftOver = LeftOver % 10;
	}
	else if (LeftOver >= NickelCoin)
	{
		NickelCoin = LeftOver / 05;
		LeftOver = LeftOver % 05;
	}
	else if (LeftOver >= PennyCoin)
	{
		PennyCoin = LeftOver / 01;
		LeftOver = LeftOver % 01;
	}
		
}

Now walk through it with $2.56 for amount. Now, aside from you if-else if problem, why are you dividing by 1 to get the number of dollars and 25 toget the number of quarters. And for $2.56, is amount 256.0 or 2.56? You'd better make sure that you're comparing dollars to dollars and pennies to pennies and not mixing them.

But your first task is to get rid of anything that looks like this (see red):

void CoinChanger::Calculate(int DollarCoin, int QuarterCoin, int DimeCoin, 
							int NickelCoin, int PennyCoin)

No parameters should be named the same as your class data members until you understand what is going on better.

Ditto here in your constructor:

CoinChanger::CoinChanger()
{
	int Count = 0;
	double Amount = 0.0;
	double TotalChange = 0.0;

	int DollarCoin = 0;
	int QuarterCoin = 0;
	int DimeCoin = 0;
	int NickelCoin = 0;
	int PennyCoin = 0;

	Calculate(DollarCoin, QuarterCoin, DimeCoin, 
			 NickelCoin, PennyCoin);
}

If you have a variable type declaration like int inside of your implementation function, it is NOT a class member variable. It is a local variable that has the same name. You already specified the type in your header file. Delete the "int" or change the variable name.

Thank you very much! Very detailed, concise, and organized. I will work on the format of my layout, and based on what you said I already see several issues hampering the logic of the code.

I appreciate it.

Thank you very much! Very detailed, concise, and organized. I will work on the format of my layout, and based on what you said I already see several issues hampering the logic of the code.

I appreciate it.

You're welcome. Just noticed your signature. Food for thought. A liberal just helped you for free, so I guess that makes you a welfare recipient! ;) Good luck!

You're welcome. Just noticed your signature. Food for thought. A liberal just helped you for free, so I guess that makes you a welfare recipient! ;) Good luck!

LOL, :D yes Sir. I do believe you are correct!

Thank you once again.

Hi Sir

When I use the code : CoinChanger::CoinChanger(), it appear the error message " expected function body after function declarator" so could you please advise me regarding this matter.

Thank you

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.