Hello;

I need to develop a money class that will simulate the cash box of a register. The cash box will contain coins and currency in the following denominations: \$100, \$50, \$20, \$10, \$5, \$1, \$0.25, \$0.10, \$0.05, and \$0.01.

I am barely new in C++. I couldn't figure out the actual algorithm and how to simulate a program which i haven't done before

I use a class that will make use of the struct

``````struct Cash {

int iMoneyArray[10];

};``````

.

The array members provide the number of that domination in the cash box (not the value) as follows:
Index 0. Number of 100 dollar bills
Index 1. Number of 50 dollar bills
Index 2. Number of 20 dollar bills
..
..
..
Index 9. Number of pennies

My first constructor has no parameters (default constructor) and sets all the values of the cash drawer to zero (the cash box is empty)

second constructor is used to set all the values of the cash box to the values coming from the calling program. My prototype is:

``Money(Cash cashIn);``

I use

``Cash AddCash(Cash cashIn);``

to add cash to an existing instance of Money. It returns the current value of the cash in the cash drawer, in a Cash struct, after the cash has been added.

``int GetCashAmountInt( );``

I use this to return the current value, in pennies, of the cash in the cash box.

``Cash GetCashAmount();``

and this returns the cash in the cash box in a Cash struct.

``int MakeChange(int iPurchaseAmount, Cash cashIn, Cash & cashOut);``

I use it to take in a purchase amount, the cash offered to pay for the purchase, and returns the change needed on the transaction. It also returns an int to signify whether an error has occurred (listed below). This method makes use of the Cash struct to pass back the amount of change returned.

The definitions of the parameters and return values are as follows:

iPurchaseAmount. The purchase amount in pennies.

Cash cashIn. Uses the array to signify how much cash was given to the instance of Money class as payment.

Cash & CashOut.
The change received back from the instance of the Money class in a 'Cash' struct passed by reference. It is the difference between the money received minus the purchase price.

int return value. The method returns an integer to signify the status of the transaction:

Return 0. There is no error and the transaction was successfully completed.

Return 1. There is not enough change in the cash box to make change. The transaction is canceled and no changes are made to the cash box. Upon returning from the method the value of the cashOut will equal the value of the cashIn.

Return 2. The customer did not provide enough cash in the cashIn struct to cover the purchase price of the item. The transaction is canceled and no changes are made to the cash box. Upon returning from the method the value of the cashOut will equal the value of the cashIn.

``string ToString();``

to return the current value of all the cash in the cash box as a string in a dollar and cents notation, for example '\$ 365.45'.

Thats what i ve figured out so far but i cannot get them together. I loved doing programming but i got stuck in this and i dont want to give up after spending weeks.

What exactly do you need help with? Maybe a kick start would help. Here is some ideas:

``````class Cash{
private:
int money[10];
public:
enum {PENNIES,NICKELS,DIMES,QUARTERS, ONE_DOLLAR,FIVE_DOLLAR,TEN_DOLLAR,FIFTY_DOLLAR,ONE_HUNDRED_DOLLAR
public:
Cash(){
for(int i = 0; i < 10; ++i) money[i] = 0;
}
void subtract(const Cash& money){ /* Add code here */}
int& get(int moneyIndex){ return money[moneyIndex]; }
int getTotalCashInPennies(){/*code goes here*/}
};

class Registar{
private:
Cash initialCash;
public:
int MakeChange(int iPurchaseAmount, Cash cashIn, Cash & cashOut);
//more stuff here
};``````

I'm not sure if thats similar to how your requirements are but that should give you an idea.

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.