if...else print outs

Hi,

I am running a loop 3 times, and each iteration has 3 decisions to make.

My problem occurs in my function 'calculateCharges'. The 'if' structure falls into 3 categories for the user to enter:

0 to 3
>3 to 10
>10 on

The problem seems to occur when I try to enter hours parked greater than 10 as seems to also be calling the >3 to 10 option as well, as shown by printing out:

"2nd one"
"3rd one"

where it should only print out "3rd one".

I am not sure if my 'if', 'else if', statements are being used right, but I've tried other combos and doesn't seem to work too well as it seems to give run-time errors. Can ideas?

----
// Enter hours parked for each car
// A car an park more than 10 hours, but it cannot be charged hire than the max fee
// $2.00 min parking fee for up to 3 hours
// $0.50 for each extra hour, or part thereof for each additional hour
// Calculate and print parking charges for each of 3 customers who parked their cars in the garage
// Calculate and print total of parking receipts


#include <iostream>
#include <string>
#include <iomanip> // std::setprecision

using namespace std;

float calculateCharges (float hours_parked)
{
float fee_current; // cost of parking a car

if ( hours_parked <= 3 )
{
// fee is a minimum of $2 for 3 hrs parked or less
fee_current = 2;
cout << "1st one" <<endl;
}

else if ( 3 < hours_parked <= 10 )
{
fee_current = 2 + ((hours_parked - 3) * 0.5); // extra hours over 3 hours parked
cout << "2nd one" <<endl;
}

if ( 10 < hours_parked )
{
fee_current = 2 + ((10 - 3) * 0.5); // up to 10 hours parked is max charge allowed
cout << "3rd one" <<endl;
}

return fee_current;
}

int main()
{
float hours_parked;
int counter;
float fee_running = 0;
float fee_current = 0;
float fee_total = 0;
float car_1, car_2, car_3 = 0;

for (counter = 1; counter < 4; counter++) // later change to a variable, if possible, which can be set to how many you cars you want to enter
{
cout << "Enter the number of hours parked: ";
cin >> hours_parked;


fee_current = calculateCharges(hours_parked);
cout << "\nThis car parked " << fixed << setprecision(2) << hours_parked << " hour(s)." << endl;
cout << "\nCost for this car is $" << fixed << setprecision(2) << fee_current << endl;
cout << "\n\n" << endl;

fee_running = fee_running + fee_current; // charged fee for current car

//assign current charged fee of car to a static name to be printed later
if (counter == 1)
{ car_1 = fee_current; }

else if (counter == 2)
{ car_2 = fee_current; }

if (counter == 3)
{ car_3 = fee_current; }

}

fee_total = fee_running;

cout<< "car_1 = $ " << fixed << setprecision(2) << car_1 << endl;
cout<< "car_2 = $ " << fixed << setprecision(2) << car_2 << endl;
cout<< "car_3 = $ " << fixed << setprecision(2) << car_3 << endl;

cout <<"\nTotal = $ " << fee_total << "\n" << endl;
cout << "END" << endl;

cin.ignore();
cin.get();

return 0;
}
Instead of

else if ( 3 < hours_parked <= 10 )

you should write

else if ( 3 < hours_parked && hours_parked <= 10 )
Also instead of these three if-else statements

if ( hours_parked <= 3 )
{
//...
}

else if ( 3 < hours_parked <= 10 )
{
// ...
}

if ( 10 < hours_parked )
{
// ...
}

it would be much better to write

if ( hours_parked <= 3 )
{
// ...
}
else if ( hours_parked <= 10 )
{
// ...
}
else
{
//...
}
Last edited on
Thank you - much appreciated!
Topic archived. No new replies allowed.