parking fee programming code

Hi there. I have an upcoming test about parking ticket fee and i think i have succesfully created a full coding. But then it does not calculate the total fee of it. Can anyone please help me on how to solve this issue?

//First hour rate (24 hours) : RM 5
//Office hour rate (8 am to 5 pm) : RM 3
//After Office Rate : RM 4
//Saturday, Sunday and Public Holiday (Flat Rate): RM 2 per hour

#include <iostream>
using namespace std;
int main()
{
float day;
float timein;
float timeout;
float totalpay;
float totaltime;
float totaltime2;
float totaltime3;

cout<<"WELCOME TO LOT 1 PARKING AREA"<<endl<<endl;
cout<<"Please enter the day today."<<endl;
cout<<"1 for Monday"<<endl;
cout<<"2 for Tuesday"<<endl;
cout<<"3 for Wednesday"<<endl;
cout<<"4 for Thursday"<<endl;
cout<<"5 for Friday"<<endl;
cout<<"6 for Saturday"<<endl;
cout<<"7 for Sunday"<<endl;
cout<<"8 for Public Holiday"<<endl;
cout<<"Day:";
cin>>day;

cout<<"Enter your time in here (24-hours system):";
cin >> timein;

cout<<"Enter your time out here (24-hours system):";
cin >>timeout;

if (timein >= 8 && timeout <= 17 && timeout > timein) //office hour rate
{
totaltime = timeout - timein;

if (day == 1 || day == 2 || day == 3 || day == 4 || day == 5)
{
{
if (totaltime == 1)
{
totalpay = 5;
cout<<"Please pay RM a "<<totalpay<<endl;
}

else
{
totalpay = ((totaltime - 1) * 3) + 5;
cout<<"Please pay RM b "<<totalpay<<endl;
}
}
}

if (timein >= 18 && timeout <= 24 && timeout > timein) //other than office hour
{
totaltime2 = timeout - timein;

if (day == 1 || day == 2 || day == 3 || day == 4 || day == 5)
{
{
if (totaltime2 == 1)
{
totalpay = 5;
cout<<"Please pay RM d "<< totalpay <<endl;
}

else
{
totalpay = ((totaltime2 - 1) * 4) + 5;
cout<<"Please pay RM e "<< totalpay <<endl;
}
}
}
if (timein >= 1 && timeout <= 7 && timeout > timein) //other than office hour & morning
{
totaltime3 = timeout - timein;

if (day == 1 || day == 2 || day == 3 || day == 4 || day == 5)
{
{
if (totaltime3 == 1)
{
totalpay = 5;
cout<<"Please pay RM g "<< totalpay <<endl;
}

else
{
totalpay = ((totaltime3 - 1) * 4) + 5;
cout<<"Please pay RM h "<< totalpay <<endl;
}
}
}
else if (day == 6 || day == 7 || day == 8) // Saturday & Sunday & Public Holiday
{
if(totaltime3 == 1)
{
totalpay = 5;

cout<<"The total parking fee is RM "<<totalpay<<endl;
}

}
}

}
return 0;
}
}
Last edited on
your conditions are wrong.
first, there is no response on 6/7/8 when time is not 1.
second, it does not get into 6/7/8 anyway due to the if/else spaghetti.
at this point you can either do-over and rethink your logical branching and conditions carefully this time, or you can use the __LINE__ macro to debug where it gets for each test case and fix problems as you find them (and you likely will NOT get all the problems without extensive testing).

Lets take this one:
input 6, 10, 15 (holiday, 5 hour parking)
...
it ends up at the first condition:
if (timein >= 8 && timeout <= 17 && timeout > timein) //office hour rate
{
and then what?

if (day == 1 || day == 2 || day == 3 || day == 4 || day == 5) this fails, its 6th day
//side node, what about day < 6 instead? 5 times more work to check one by one
... not here
jumps to
if (timein >= 18 && timeout <= 24 && timeout > timein) //other than office hour
... not here
}
jumps to
return 0 // nothing printed, nothing done!

you have the right idea. you need a way to do the work for each possible scenario, and you did that, its just not wired up correctly. It could be a bracket {} problem or it could be overlapping conditions/assumptions problems, or a mix of the two. A do-over won't take as long as you think. Sit down with a piece of paper and figure out the scenarios. you have weekday, office hours. You have weekday, other hours. You have not-weekday, (any hours? or do office hours matter on these days?). Three blocks, 4 if office hours matter on non weekdays. And those 4 blocks are redundant, so you can probably make a little function to do the dirty work, with a parameter or two, to make it easier to follow.
Last edited on
Here is a grossly oversimplified starting point to think about.
also, code tags, use them and it is indented and formatted etc and you can even test it here in the forums. Please use them!

so what did I do here? I figured out the rate up front, and then made a parameter for your output statement so the statement changes to fit the data. So the bulk of the work is just finding the correct rate, which I am not 100% sure this below does, but its a starting point. I don't know what you want to do for overnight parking or like midnight to 5 am parking, or partial work day (3pm) to after (7pm) type scenarios. Ill leave that logic to you... but the key is to find the rate(s) up front. even if you handle more complex things, a single statement still works, it just becomes something like rate1*(timeout1-timein1)+ rate2*(timeout2-timein2)+... some of those may be zeros and add nothing, but that is ok.

I also used a string as a single character lookup to get the codes (but not correctly). Just load the string with the letter for the related rate, eg if rage is 4 and code is 'h' the 5th letter (remember, from zero, not from 1) 01234 <-4 is the 5th letter here ... and then string[rate] will give you the letter you want back out. If it is that simple. If the letters are more complex, you can set a char variable in your logic as you unravel the rates, that works too, its just more code.


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
#include <iostream>
using namespace std;
int main()
{
int day;
float timein;
float timeout;
float totalpay;
float totaltime;
float totaltime2;
float totaltime3;

cout<<"WELCOME TO LOT 1 PARKING AREA"<<endl<<endl;
cout<<"Please enter the day today."<<endl;
cout<<"1 for Monday"<<endl;
cout<<"2 for Tuesday"<<endl;
cout<<"3 for Wednesday"<<endl;
cout<<"4 for Thursday"<<endl;
cout<<"5 for Friday"<<endl;
cout<<"6 for Saturday"<<endl;
cout<<"7 for Sunday"<<endl;
cout<<"8 for Public Holiday"<<endl;
cout<<"Day:";
cin>>day;

int rate{5}; //default: first hour rate
if (timein >= 8 && timeout <= 17 && day <6 && timeout > timein) //office hour rate
rate = 3;
else if(timein > 17)
rate = 4;
if(day >5)
rate = 2;

string ratecodes ="xxabcdefgh"; //I have no idea what the a-h code need to be, this is an example, you need to fix it. 

cout<<"Enter your time in here (24-hours system):";
cin >> timein;

cout<<"Enter your time out here (24-hours system):";
cin >>timeout;

cout << "Please pay RM " << ratecodes[rate] << " " << (timeout-timein)*rate;

//First hour rate (24 hours) : RM 5
//Office hour rate (8 am to 5 pm) : RM 3
//After Office Rate : RM 4
//Saturday, Sunday and Public Holiday (Flat Rate): RM 2 per hour


return 0;
}
Last edited on
thank you very much for your explanations! for the a-h codes, it is just labels so I won't get confused once I run the program. it doesn't really represents anything. I understand now and this is so much easier to think off. Thank you very much good sir. I hope you have a nice day today and the upcoming days!
Last edited on
also can you give me some tips on how to enhance my coding knowledge more?
Writing code is the best way until you get the basics down - loops, logic, functions, etc. Once you have those down, a study of algorithms and data structures is good for understanding, even though c++ has a lot of the common ones built in. Then look at a study of object oriented ideas.
At each phase though writing code is the best way to learn it, and if something feels overly complex** for the problem at hand, post here and ask how it can be better. Read the web on a few different common coding styles too, variable names, indentations, comments and how to make the code presentable.

Over and over the coders here recommend https://www.learncpp.com/

** Most problems for schoolwork or practice etc can be done in half a page until you get a long way into OOP and bigger projects. Smaller code isnt always better: it can often be hard to read and hard to debug, but giant code is hard to follow and debug too. Find a happy place between where the code is readable and only as complicated as the problem merits.
Last edited on
thank you very much good sir. may everything will be eased for you and have a good day! :)
Topic archived. No new replies allowed.