Function for Addition displaying result as 1.#R

Jun 16, 2013 at 1:41am
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;
}
Jun 16, 2013 at 1:57am
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?
Jun 16, 2013 at 2:08am
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;
Jun 16, 2013 at 2:14am
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.
Jun 16, 2013 at 2:22am
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)
Jun 16, 2013 at 2:24am
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?
Jun 16, 2013 at 2:33am
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.