functions, passing by reference

So i am writing this code for my computer science class and i am stuck.

It is supposed to do this:

Enter a total: 20.31
Enter amount tendered: 22.00
Your change is $1.69.

You should have 2 quarters, 1 dime, 1 nickel, and 4
pennies.

This is what i have so far

#include <iostream>
using namespace std;

double get_money(string);

// need help with this function
void calc_change();

int main()
{
double total, tendered;
total = get_money("Enter a total: ");
tendered = get_money("Enter amount tendered: ");
cout << "your change is " << calc_change(total, tendered) << endl;
cout << "you should have " << calc_change(change) << endl;
return 0;
}

double get_money(string prompt)
{
double num;
cout << prompt << endl;
cin >> num;

while(num <= 0)
{cout << "enter a positive number." << endl;
cin >> num;}

return num;
}

void calc_change(double change, int & quarters,int & dimes, int & nickels, int & pennies)
{
// need help with this function
//
//
}

Any help would be appreciated.
First of all what do you want calc_change() to do? Do you want it to calculate how much change you should get using total and tendered (if yes, as parameters to the function or use the total and tendered defined in main?), or calculate how many quarters, dimes, nickels and pennies you should get using the change amount passed in?

You could overload the function as
double calc_change(double total, double tendered);
and
void calc_change(double change, int &quarters, int &dimes, int &nickels, int &pennies);,
One calculating the change amount and one calculating the amount of quarters, dimes, nickels and pennies, if you want the function to do both. But this is bad practice since this overloaded calc_change() has two purposes that differ a lot.
For the first purpose you can just do
1
2
3
inline double calc_change(double total, double tendered) {
    return tendered - total;
}


The second one is a little bit more tricky...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void calc_change(double change, int &quarters, int &dimes, int &nickels, int &pennies) {\
    //set all to zero to avoid errors
    quarters = 0;
    dimes = 0;
    nickels = 0;
    pennies = 0;
    //ignore the whole dollar part. we do so by using modulus
    //same as change = change % 1.0, but because % can only perform modulus on
    //two ints, we use fmod
    change = fmod(change, 1.0);
    //we use 0.001 as the limit instead of zero because of floating-point rounding errors
    while(change > 0.001) {
        if(change >= 0.25) {//the (remaining) change is more than a quarter
            quarters ++;//add one quarter to output
            change -= 0.25;//remove one quarter from (remaining) change
        }
        //same as above
        //note that we use else if to make sure the max amount of quarters/dimes/nickels is
        //reached before we move on to dimes/nickels/pennies
        else if(change >= 0.10) {
            dimes ++;
            change -= 0.10;
        }
        else if(change >= 0.05) {
            nickels ++;
            change -= 0.05;
        }
        else if(change >= 0.01) {
            pennies ++;
            change -= 0.01;
        }
    }
}


I've never tested this but hopefully it would work. My only worry is that maybe sometimes things could go wrong because of floating-point rounding errors.
hmm
Last edited on
P.S. I just thought of it, since the least amount of money more than 0 is one penny or $0.01, you can make the money variables ints to avoid floating-point precision errors. It's just you have to change the code a little bit and divide it by 100 when you cout it, but that's totally worth it in big programs involving money when you can't afford to have even the slightest error (like, a bank program).
i want calc_change to calculate how many coins will be needed as change.

What would i put in the prototype?

I have not learned what "inline" is used for. i appreciate the help.
Prototype would be void calc_change(double change, int &quarters, int &dimes, int &nickels, int &pennies).
Actual code would be
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
void calc_change(double change, int &quarters, int &dimes, int &nickels, int &pennies) {
    //set all to zero to avoid errors
    quarters = 0;
    dimes = 0;
    nickels = 0;
    pennies = 0;
    //ignore the whole dollar part. we do so by using modulus
    //same as change = change % 1.0, but because % can only perform modulus on
    //two ints, we use fmod
    change = fmod(change, 1.0);
    //we use 0.001 as the limit instead of zero because of floating-point rounding errors
    while(change > 0.001) {
        if(change >= 0.25) {//the (remaining) change is more than a quarter
            quarters ++;//add one quarter to output
            change -= 0.25;//remove one quarter from (remaining) change
        }
        //same as above
        //note that we use else if to make sure the max amount of quarters/dimes/nickels is
        //reached before we move on to dimes/nickels/pennies
        else if(change >= 0.10) {
            dimes ++;
            change -= 0.10;
        }
        else if(change >= 0.05) {
            nickels ++;
            change -= 0.05;
        }
        else if(change >= 0.01) {
            pennies ++;
            change -= 0.01;
        }
    }
}

This would work, hopefully.

Don't worry about inline too much for now. You'll eventually learn it. It's because jumping from one function to another in the call stack takes time, and for functions that have only one or few lines the time jumping from function to function is even longer than the function itself. If a function is marked as inline, when the compiler compiles the code of the function, it literally copies the code from the function into the function calling it. Example:
1
2
3
4
5
6
inline double calc_change(double total, double tendered) {
    return tendered - total;
}
//somewhere in main()...

cout << calc_change(total, tendered) << endl;

would compile as if it has been written like this:
1
2
//somewhere in main()...
cout << (tendered - total) << endl;


Sorry if I got you confused. But don't worry about it for now. It only affects the performance of the program by a little, and in some compilers it automatically compiles functions as inlines if the function only contains little code.
Topic archived. No new replies allowed.