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.
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
elseif(change >= 0.10) {
dimes ++;
change -= 0.10;
}
elseif(change >= 0.05) {
nickels ++;
change -= 0.05;
}
elseif(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.
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).
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
elseif(change >= 0.10) {
dimes ++;
change -= 0.10;
}
elseif(change >= 0.05) {
nickels ++;
change -= 0.05;
}
elseif(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:
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.