Function Parameter Error
Feb 1, 2014 at 8:11pm UTC
Hi, this program is supposed to output a table, which it does do. however, the values in the last column are incorrect. I think it has something to do with my function parameters, as the function calls use different variable names than those in the function declaration and definition. I have seen code where this is done, but I suspect that I am not doing it right.
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/* This program calculates the individual
and total charges for three cars in a
parking garage depending on how many
hours each car was parked. The program
takes in the data and outputs the charges
in a table.
*/
#include <iostream>
using namespace std;
void CalculateCharge(double & hours, double & charge);
int main()
{
double car1 = 0, car2 = 0, car3 = 0, charge1 = 0, charge2 = 0, charge3 = 0, TotalHours = 0, TotalCharge = 0;
cout << "Welcome to the parking garage program." << endl;
cout << "Enter the hours parked for three cars:" << endl;
cin >> car1;
cin >> car2;
cin >> car3;
CalculateCharge(car1, charge1);
CalculateCharge(car2, charge1);
CalculateCharge(car3, charge1);
TotalHours = car1 + car2 + car3;
TotalCharge = charge1 + charge2 + charge3;
cout << "Car" << "\t\t" << "Hours" << "\t\t" << "Charge" << endl;
cout << "1" << "\t\t" << car1 << "\t\t" << charge1 << endl;
cout << "2" << "\t\t" << car2 << "\t\t" << charge2 << endl;
cout << "3" << "\t\t" << car3 << "\t\t" << charge3 << endl;
cout << "Total" << "\t\t" << TotalHours << "\t\t" << TotalCharge << endl;
return 0;
}
void CalculateCharge(double & hours, double & charge)
{
if (hours > 3)
{
charge = 2 + (0.5 * (hours - 3));
}
else
{
charge = 2 + (0.5 * hours);
}
}
Feb 1, 2014 at 8:25pm UTC
Can you please give an example showing the program output and what output you expected.
Feb 1, 2014 at 8:33pm UTC
You have set all of the 2nd parameters to charge1
1 2 3
CalculateCharge(car1, charge1);
CalculateCharge(car2, charge1);
CalculateCharge(car3, charge1);
guessing thats where youre running into trouble
hope that helps :)
Feb 1, 2014 at 8:54pm UTC
Thanks, that does help.
Topic archived. No new replies allowed.