THIS IS THE QUESTION.

1- The user would enter how many minutes he/she would like to park in the parking lot.
2- The program would output the parking cost.
3 - The rate charge by the parking lot is as follows

First 3 hours - RM4/Hour

Hours 9 onwards - RM2/Hour

I ALSO NEED TO CONSIDER TO:
1. Parking for 120 minutes is considered 2 hours and therefore would cost RM 8 (2 hour x RM 4)
2. Parking for 121 minutes is considered 3 hours and therefore would cost RM 12 (3 hour x RM 4)
3. Parking for 190 minutes is considered 4 hours and therefore would cost RM 15 (3 hours x RM 4 + 1 hour x RM 3)
4. Parking for 490 minutes is considered 9 hours and therefore would cost RM 29 (3 hours x RM 4 + 5 hours x RM 3 + 1 hour x RM 2)

SO ,, THIS IS WHAT I AM TRYING TO DO,,BUT FAILED..

``````#include <iostream>

using namespace std;

int main()
{
float minute, cost, hours1= 4, hours2= 3, hours3= 2;

cout<<"Parking fees :"<<endl;
cout<<"***************************"<<endl;
cout<<"First 3 hours   - RM4/Hour "<<endl;
cout<<"Hours 4 to 8    - RM3/Hour "<<endl;
cout<<"Hours 9 onwards - RM2/Hour "<<endl;

cout<<"************************************************"<<endl;
cout << "Please insert your parking period (in minute):"<<endl;
cin >> minute;

// process from hour to minutes
float hour = minute / 60;

if(hour <= 2){
cost = hour * hours1;
}

else if(hour >= 3 && hour <=4){
cost = hour * hours1;
}

///CALCULATION OF
// Parking for 190 minutes is considered 4 hours and therefore would cost
// RM 15 (3 hours * RM 4 + 1 hour * RM 3)
else if(hour > 4 && hour <= 8){
cost = (3 * hours1) + (1 * hours2 );
}
//CALCULATION OF
// Parking for 490 minutes is considered 9 hours and therefore would cost
// RM 29 (3 hours * RM 4 + 5 hours * RM 3 + 1 hour * RM 2)
else if( hour > 8){
cost = (3 * hours1) + ( 5 * hours2 ) + ( 1 * hours3);
}

cout << " Your total cost for parking is RM" << cost << endl;

return 0;
}
``````

First up, you've posted in the wrong forum. You've posted C++ code in the C forum, heh heh!

Anyway, as you are dealing with whole hours, you might want to consider making hours an int. Also, to take into account somebody parking for a fraction of an hour you should use std::ceil in your calculation.

So for example 490 minutes equates to 8.16667 hours, std::ceil will round this value up to 9.0. So they will be charged for 9 hours rather than 8.
Likewise, if the user entered 20 minutes, std::ceil will round that up to 1.00, so they get charged for 1 hour.

So perhaps for your calculation for the number of hours parked:
`int hoursParked = static_cast<int>(std::ceil(minutes/60));`
In the above we use std::ceil to round-up the value of minutes/60 and we use a C++ static cast to cast the value from float to int.
Note: std::ceil requires you to `#include <cmath>`

Also your variables hours1, hours2 and hours3 are badly named as they do not describe the values they contain. These variables do not store hours, they store the rates at which hours of parking are charged. So personally I would call them rate1, rate2 and rate3.

As for calculating the cost of parking, your calculations are simply wrong!
As your code stands, if a user was parked for between 4 and 8 hours they would only be charged for 4 hours.
Likewise, if a user was parked for anything over 8 hours, they would only be charged for 8.

For any given number of hours, start from the highest rate and work down to the lowest rate and determine the number of hours at each rate and adjust the cost accordingly.

So for the calculated number of hours:
1. Calculate how many hours are at rate 3, calculate the cost of those hours and add this to the total cost.
2. Calculate how many hours are at rate 2, calculate the cost of those hours and add this to the total cost.
3. Calculate the cost of the remaining hours at rate 1 and add this to the total cost.

Think about it, break the problem down step by step. I don't think I can say much more without giving you an answer or spoonfeeding code!