-
Such a simple problem
Title says its all!
Cannot figure out why when I print "totalFee" im getting 0.00 as output although I am passing it by reference to the function. At no point can I see why the variable is not adding the "charge" value. I am not getting errors or warnings. It looks like quite a small mistake im making.
Can anyone see what I am doing wrong?
Code:
#include <iostream>
#include <iomanip>
// function prototype
double calculateCharge ( double, double& );
// main function - driver //////////////////////////////////////////////////////
//
int main ( void )
{
std::cout << std::setprecision ( 2 ) << std::fixed;
double time1 = 0,
time2 = 0,
time3 = 0,
totalTime = 0,
totalFee = 0;
std::cout << "Enter stay times for three cars: ";
std::cin >> time1 >> time2 >> time3;
totalTime = time1 + time2 + time3;
std::cout << "\nCAR\tSTAY\tFEE\n\n"
<< " 1\t" << time1 << "\t" << calculateCharge ( time1, totalFee )
<< "\n 2\t" << time2 << "\t" << calculateCharge ( time2, totalFee )
<< "\n 3\t" << time3 << "\t" << calculateCharge ( time3, totalFee )
<< "\n\nTotal Time: " << totalTime
<< "\nTotal Fee: " << totalFee << std::endl;
std::cin.get(); // freeze console output window
std::cin.ignore();
return 0; // return value from int main
}
// function to calculate the charge
// for each car stayed
double calculateCharge ( double x, double &rFee )
{
double charge = 0;
if ( x <= 3.00 )
{
charge = 2.00;
rFee += charge;
}
if ( x > 3.00 )
{
double timeOverStandard = 0,
additionalCharge = 0;
timeOverStandard = x - 3.00;
additionalCharge = timeOverStandard * 0.5;
charge = 2.00 + additionalCharge;
rFee += charge;
}
if ( x == 24.00 )
{
charge = 10.00;
rFee += charge;
}
return charge;
}
-
Elementary my dear Watson. One possible fix... change:
Code:
std::cout << "\nCAR\tSTAY\tFEE\n\n"
<< " 1\t" << time1 << "\t" << calculateCharge ( time1, totalFee )
<< "\n 2\t" << time2 << "\t" << calculateCharge ( time2, totalFee )
<< "\n 3\t" << time3 << "\t" << calculateCharge ( time3, totalFee )
<< "\n\nTotal Time: " << totalTime
<< "\nTotal Fee: " << totalFee << std::endl;
To:
Code:
std::cout << "\nCAR\tSTAY\tFEE\n\n"
<< " 1\t" << time1 << "\t" << calculateCharge ( time1, totalFee )
<< "\n 2\t" << time2 << "\t" << calculateCharge ( time2, totalFee )
<< "\n 3\t" << time3 << "\t" << calculateCharge ( time3, totalFee )
<< "\n\nTotal Time: " << totalTime;
std::cout << "\nTotal Fee: " << totalFee << std::endl;
I'll let you think about the "why".
-