Function for Addition displaying result as 1.#R

I have a user-defined function that calls two other user-defined functions and when the console displays "totalBill" it looks like this:

1.#R

The carCharge and mileageCharge calculate and display properly.

1
2
3
4
5
6
7
8
9
double CalculateTotalCharge (double RentalTax, char carSize, int milesDriven, int daysRented, double& carCharge, double& mileageCharge, double& taxCharged, double& totalBill) 
                             
{
   carCharge = CalculateCarCharge (carSize, daysRented, CompactRate);     
   mileageCharge = CalculateMileageCharge (carSize, milesDriven, CompactMileageOver, MidSizeMileageOver, FullSizeMileageOver);
   
   taxCharged = (carCharge + mileageCharge) * RentalTax;
   totalBill = carCharge + mileageCharge + taxCharged;
}
You function states that it will return a double but you haven't listed a return statement.

Have you considered grouping all those parameters into a struct or class so you can just pass a single argument?
We are not supposed to use anything other than a return statement or reference parameters.

I know I can't return two values with a return statement. I thought both values would get passed back to main by using the reference parameters in the function statement. Am I wrong about that?

Do I need to do this to make the reference parameters work?
cin >> taxCharged;
cin >> totalBilled;
No, you can't return two values with a return statement but... You can return an object...An object that contains as many values as memory allows.

You can also pass in an object by reference.
I thought I was passing the variables back by reference in the function statement:
 
double CalculateTotalCharge (double RentalTax, char carSize, int milesDriven, int daysRented, double& carCharge, double& mileageCharge, double& taxCharged, double& totalBill)
Are you saying the last four parameters of your function are what you are "returning?" If so, then you don't need a return type. Change it to a void function if you are indeed returning through your reference parameters.

As for your strange output, why don't you show us where you call the function and where the output is displayed?
That was it. I was treating this as a value returning function.

When I changed it to a void the calculations display perfectly.

Thank you so much. Such a simple mistake can cause so much churn!
Topic archived. No new replies allowed.