### 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..

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950`` ``````> 1. #include 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 :"<> 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 )...`

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445464748`` ``````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

 ``1234`` ``````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

 ``1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253`` ``````#include #include 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 :"<> 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.

#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..

 ``123456789101112131415161718192021222324252627282930313233343536373839404142434445`` `````` #include using namespace std; int main() { int minute; int cost; cout<<" ***** PARKING FEE *********"<> 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; } ``````