guide me to solve parking rate..

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 - USD4/Hour
Hours 4 to 8 - USD3/Hour
Hours 9 onwards - USD2/Hour

I ALSO NEED TO CONSIDER TO:
1. Parking for 120 minutes is considered 2 hours and therefore would cost USD 8 (2 hour x USD 4) -- DONE
2. Parking for 121 minutes is considered 3 hours and therefore would cost USD 12 (3 hour x USD 4) -- DONE.

This is i am lack ..
3. Parking for 190 minutes is considered 4 hours and therefore would cost USD15 (3 hours x USD 4 + 1 hour x USD 3)
4. Parking for 490 minutes is considered 9 hours and therefore would cost USD 29 (3 hours x USD4 + 5 hours x USD 3 + 1 hour x USD 2)

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




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
>
1.	#include <iostream>
2.	
3.	using namespace std;
4.	
5.	int main()
6.	{
7.	    float minute, cost, hours1= 4, hours2= 3, hours3= 2;
8.	
9.	    cout<<"Parking fees :"<<endl;
10.	    cout<<"***************************"<<endl;
11.	    cout<<"First 3 hours   - USD4/Hour "<<endl;
12.	    cout<<"Hours 4 to 8    - USD3/Hour "<<endl;
13.	    cout<<"Hours 9 onwards - USD2/Hour "<<endl;
14.	
15.	    cout<<"************************************************"<<endl;
16.	    cout << "Please insert your parking period (in minute):"<<endl;
17.	    cin >> minute;
18.	
19.	    // process from minutes to hours
20.	    float hour = minute / 60;
21.	
22.	    if(hour <= 2){
23.	        cost = hour * hours1;
24.	    }
25.	
26.	    else if(hour >= 3 && hour <=4){
27.	        cost = hour * hours1;
28.	    }
29.	
30.	    ///CALCULATION OF
31.	    // Parking for 190 minutes is considered 4 hours and therefore would cost
32.	    // USD 15 (3 hours * USD 4 + 1 hour * USD 3)
33.	    else if(hour > 4 && hour <= 8){
34.	        cost = (3 * hours1) + (1 * hours2 );
35.	    }
36.	    //CALCULATION OF
37.	    // Parking for 490 minutes is considered 9 hours and therefore would cost
38.	    // USD 29 (3 hours * USD 4 + 5 hours * USD 3 + 1 hour * USD 2)
39.	    else if( hour > 8){
40.	        cost = (3 * hours1) + ( 5 * hours2 ) + ( 1 * hours3);
41.	    }
42.	
43.	    cout << " Your total cost for parking is USD" << cost << endl;
44.	
45.	     return 0;
46.	}




pls guide me as simple as you can,, i am beginner.. :(
Last edited on
The if statements on line 22 and 26 can be combined into one if-statement as follows:
if ( hour > 3 )...

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
float calculateParking( float hours )
{
	float cost = 0;
	if ( hours > 3 ) 
		cost += ( 12 );
	else {
		cost += ( hours * 4 );
		return cost;
	}
	hours = hours - 3;	
	if ( hours > 5 ) 
		cost += ( 15 );
	else {
		cost += ( hours * 3 );
		return cost;
	}
	
	hours = hours - 5;
	cost += ( hours * 2 );
	
	return cost;
}
/*     First we check if the time is greater than 3
	if it is, we take 3 hours from the time and
	multiply by 4 - this will always result in 12
	then add it cost
	else
	multiply the time by 4 and this is your cost
	return cost
	
	if we did not do the else part in the above 
	if-statement, this means we are still in the 
	function. So subtract 3 from the hours
	
	Since we subtracted 3, we cannot use 8 as 
	our next check so we use 8-3 (5)
	if hours is greater than 5, we take out 5 from the
	hours and multiply by 3 -this will always result in 15
	add 15 to cost
	else
	this means the time given is less than 8 hrs,
	so multiply the rest by 3 and add to cost
	return cost
	
	subtract 5 from the remaining hours and multiply
	the rest by 2 and return this cost. This is your
	final cost 
*/
Last edited on
merge

1
2
3
4
26.	    else if(hour >= 2 && hour <=4){
27.	        cost = hour * hours1;
28.	    }
29.	


but
sorry .. i cant understand this.

float calculateParking( float hours )
suddenly got hours?

where should i start?
redo all coding?
i am beginner...

when im trying to mix up with mine,, its have a lot of error..


do i need to redo all my code?



Last edited on
You just have to add the function to your code and use the result as your answer

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
51
52
53
#include <iostream>
#include <cmath>
using namespace std;

float calculateParking( float hours ) //http://www.cplusplus.com/doc/tutorial/functions/
{
	float cost = 0;
	if ( hours > 3 ) 
		cost += ( 12 );
	else {
		cost += ( hours * 4 );
		return cost;
	}
	hours = hours - 3;	
	if ( hours > 5 ) 
		cost += ( 15 );
	else {
		cost += ( hours * 3 );
		return cost;
	}
	
	hours = hours - 5;
	cost += ( hours * 2 );
	
	return cost;
}

int main() 
{
	
        cout<<"\n\tParking fees :"<<endl;
	cout<<"\t***************************"<<endl;
	cout<<"\tFirst 3 hours   - $4/Hour "<<endl;
	cout<<"\tHours 4 to 8    - $3/Hour "<<endl;
	cout<<"\tHours 9 onwards - $2/Hour "<<endl;
	
	cout<<"\t***************************"<<endl;
	cout << "\n\tPlease insert your parking period (in minute):\n\t";
	
	//\t is the ascii character for tab. cout << int('\t'); <---to see it's decimal representation
	//\n is the ascii character for return or new line. It does the same thing as endl
	
	float minute, hour, cost;
	cin >> minute;
	
	hour = ceil(minute/60); //http://www.cplusplus.com/reference/cmath/ceil/
	
	cost = calculateParking (hour); //Call function here and assign what it returns to the variable 'cost'
	
	cout << "\tYour total cost for parking is $" << cost << endl;
	
    return 0;
}
Last edited on
hi Smac.

i've not learn about
#include <cmath> yet ( also i know this hav connection to ceil.. thanks to you for guiding me )

so..

this is what i've done, for beginner like me , is it ok?
afterwords,, i will try like yours..

please comment. :)


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

#include <iostream>
using namespace std;
int main()
{
    int minute;
    int cost;

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

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

/** convert minutes to hour */
    double hour = (minute * 100) / 60;
    int hour1 = (minute / 60) * 100;
    int hour2;

   if (hour > hour1){
   hour2 = (minute / 60) + 1;
   }else {hour2 = (minute / 60);}


/** Process */


    if(hour2 < 4){
        cost = hour2 * 4;
    }
    if((hour2 > 3) && (hour2 < 9)){
        cost = (3 * 4) + ((hour2 - 3) * 3);
    }
    if (hour2 > 8){
        cost = (3 * 4) + (5 * 3) + ((hour2 - 8) * 2);
    }

/** Print Out The cost  */
    cout << " Your total cost for parking is RM" << cost << endl;

    return 0;
}
Last edited on
Topic archived. No new replies allowed.