EMERGENCEY! could be confusing, but help!

I get my total correctly, this is based on aeon shopping car park system, my school assignment.
THIS IS THE PARKING RATE GIVEN ONLINE. MAX CHARGE IS 6 HOURS, AFTER THAT PRICE REMAIN.
J-Card members
Monday-Friday
First hour: Free
Subsequent 2-6 hours: RM1.00
Saturday, Sunday & Public Holidays
First hour: Free
Subsequent 2-6 hours: RM2.00
Non J-Card members
Monday-Friday
First hour: Free
Subsequent 2-4 hours: RM1.00
Saturday, Sunday & Public Holidays
First hour: Free
Subsequent 2-4 hours: RM2.00


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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include<iostream>
#include <time.h>
#include<string>
#include<cmath>
using namespace std;
int process_time(int, int, int, int);
int process_total(int, char, int);
string process_day(int);
int process_balance(int, int);
void main( )
{
	char date[9];
int hours, hours_out, mins, mins_out, time_spent, total, day, balance, pay_money;
char J_card;
string fday;

//input
    cout<<"Enter day, represented by number, 8 for public holidays: ";
	cin>>day;

	cout<<"Enter time in:";
	cin>>hours>>mins;

	cout<<"Enter time out:";
	cin>>hours_out>>mins_out;

	cout<<"Are you a J_card user? Type Y/N: ";
    cin>>J_card;

	time_spent=process_time(hours, mins, hours_out, mins_out);
	total=process_total(time_spent, J_card, day);
	fday=process_day(day);
	_strdate_s(date);
	//output
	cout<<"********************************************************************************";
	cout<<"RECEIPT: "<<endl;
	cout<<"Date is: "<<date<<endl;
	cout<<"Day entered is: "<<fday<<endl;
	cout<<"Time in is: "<<hours<<":"<<mins<<endl;
	cout<<"Time out is: "<<hours_out<<":"<<mins_out<<endl;
	cout<<"Time spent is: "<<time_spent<<"min"<<endl;
	cout<<"Total price is: RM"<<total<<endl;
	cout<<"Enter your money: RM";
		cin>>pay_money;
	    balance=process_balance(total,pay_money);
	cout<<"Your balance is: RM"<<balance<<endl;
	cout<<"Thank you for coming to aeon shopping centre"<<endl;

	
system("pause");
}

//Process the total time spent
int process_time(int hours, int mins, int hours_out, int mins_out)
{
	int time_in, time_out, time_spent;

	time_in=(hours*60+mins);
time_out=(hours_out*60+mins_out);
time_spent=(time_out-time_in);
return time_spent;
}


//To process the total price
int calculateCharge(int minutesparked, int day, char jcard)
{
	const int freehours = 1;  // hours free
	const int maxweekday = 5;  // max weekday charge
	const int maxweekend = 10; // max weekend/holiday charge
	const int weekdayhourlyrate = 1; // weekday rate
	const int weekendhourlyrate = 2;  // weekend rate
	const int jcardallowedhours = 6; // JCard hours before max
	const int nonjcardallowedhours = 4; // nonJCard hours before max
	int hourscharged = 0; // hours beyond free hour
	int parkingcharge = 0; // rate to be calculated and returned by function
	
	//find number of hours over free hours
	//round up partial hours with ceil (in <cmath>)
	if (minutesparked > freehours*60)
		hourscharged=ceil((minutesparked-60)/60.0);
	
	//calculate parking charges
	if (jcard == 'y' || jcard == 'Y')
	{
		//weekday parking
		if (day==1 || day==2 || day==3 || day==4 || day==5)
		{
			if (hourscharged == 0)
				parkingcharge = 0;
			else if (hourscharged > jcardallowedhours)
				parkingcharge = maxweekday;
			else 
				parkingcharge = hourscharged * weekdayhourlyrate;
		}
		else//weekend parking
		{
			if (hourscharged == 0)
				parkingcharge = 0;
			else if (hourscharged > jcardallowedhours)
				parkingcharge = maxweekend;
			else 
				parkingcharge = hourscharged * weekendhourlyrate;
		}
	}
	else //not a jcard holder
	{
	//code here
	}
	return parkingcharge; // return calculated charge to main
}




				



//to convert day back to full words form (return 0 problem)
string process_day(int day){

switch (day){
case 1 : return"Monday";break;
case 2 : return"Tuesday";break;
case 3 : return"Wednesday";break;
case 4 : return"Thursday";break;
case 5 : return"Friday";break;
case 6 : return"Saturday";break;
case 7 : return"Sunday";break;
case 8 : return"Public Holiday";break;
default:cout<<"Error code entered!"<<endl; return 0; break;
	}
}




//process balance (No problem)

int process_balance(int total, int pay_money)
{
	int balance;

	
		if(pay_money<total)
		cout<<"Insufficient amount of money! Restart again!";
		else
			if (pay_money>total)
		balance=(pay_money-total);
		return balance;
}

Last edited on
1)The subject header is your golden opportunity to attract qualified experts' attention. Don't waste it on babble like 'Please help me' Don't try to impress us with the depth of your anguish; use the space for a super-concise problem description instead.

2) You haven't asked a question.

3) Put your code in the code formatting ( <> ) button to the right of entry box.

4) And most importantly read the sticky labelled "Welcome -- read before posting as this make you aware of the above points I've made.
Last edited on
This is my first using. So, i just wish someone could help me out, instead of typing all this, i rather u spend sometime on helping me out. The point is, this is created for people to help beginner like me, it doesnt matter the so call title or what. Oh well, guess I'm just on my own then. Anyway, thx for ur opinion.
It's not clear what you need help with?

If you can edit your post, highlight your code, then click the <> button in the format palette on the right side of the post, that will put your code in code tags. The formatting makes it a lot easier for others to read the code.
Basically, i think this is the park this is having the problem, when i remove the return 0 at the end, system keep giving me a value for total=89
The fact is, i set the parking price max is for 6 hours, therfore, impossible to get 89. My lecturer asking us to do it in function. So i separate it the process of total in a function.
@playerjun
Yes this is the beginner forum, but we do have rules here. A beginner is expected to take the time to make a thought out subject title. A beginner is expected to explain their problem thoroughly followed by the question. A beginner is expected to use code tags to help with readability. Lastly, we don't do homework. We can give pointers for you to find the tools or language features you need and to help you figure out the answer to your problem, but we won't do it for you.

@CodeGoggles
Not to knock anyone here, but expert is a word that has a strong image with it. I think it would be more appropriate to just simply say qualified programmer or experienced programmer.

Not to knock anyone here, but expert is a word that has a strong image with it. I think it would be more appropriate to just simply say qualified programmer or experienced programmer.


@BHX Specter
FYI : That was a copy and paste from the sticky at the top of the forum. From which I was referring playerjun too. I did not write that.
Last edited on
Basically, i think this is the park this is having the problem, when i remove the return 0 at the end, system keep giving me a value for total=89
The fact is, i set the parking price max is for 6 hours, therfore, impossible to get 89. My lecturer asking us to do it in function. So i separate it the process of total in a function.


1
2
//to convert day back to full words form (return 0 problem)
string process_day(int day){


Your function returns a string, so I'd probably return something like "invalid day" if the default case were encountered. And actually - I'd probably check their entry at the time the user enters it to make sure it's valid input.


The return 0 in the process total function is overwriting whatever value you actually calculate. Did you mean to have that only execute if you hit the else condition? If yes, you'd need to have brackets around the two statements here.
1
2
3
else
	cout<<"Error code entered!! Restart programme to continue";
	return 0;
Aww :)
Tq bro! U helped me found two mistakes! But the fact that i keep constantly getting same value for total no matter i enter what to calculate still not yet solve :(

If i entered the Y in input for Jcard question, my total get 89, if 'y' entered, i get 127. No matter what time in and time out i entered :(
You have a problem in process_balance.

1
2
3
4
5
6
7
8
9
10
int process_balance(int total, int pay_money)
{  int balance;

    if (pay_money<total)
        cout<<"Insufficient amount of money! Restart again!";
    else
       if (pay_money>total)
            balance=(pay_money-total);
    return balance;
}


What is the value of balance that is returned if pay_money is less than total or pay_money is equal to total?

balance is uninitialized and is only computed if pay_money > total.

IT IS HARDER TO RESPOND TO YOUR POST BECAUSE YOU ONLY PLACED CODE TAGS AROUND ONE FUNCTION AND NOT THE ENTIRITY OF YOUR CODE.
Oh~ Ya! Man, i really so clumsy in coding, no talent at all~ :(
Keep getting mistakes...
Erm, i wanna ask, if the pay_money less than total, i display out error msg, then what should i return, i just want the user know he enter not enough money.
So there's a max daily charge of 5 RM for Monday - Friday and 10 RM for S/S/H? That's the most they'd get charged if they went over the 4 or 6 hours they're allowed?
Ya ya ya!! Thats the idea!! haha
if the pay_money less than total, i display out error msg, then what should i return, i just want the user know he enter not enough money.


You have a couple of choices:
A) You could move lines 42-43 inside process_balance and then not exit until you have a valid amount.

B) You could change process_balance to a bool function and return true if balance is valid, or false if not valid. This would imply putting a loop at lines 43-44 to continue prompting until a valid amount is input. With this approach, you would need to add balance as an additional reference parameter to process_balance.
Last edited on
I think you can simplify the code a bit. The way I'd approach this is to define constant variables for the rates and times you're given. So if the parking garage decides to change its pricing structure and/or JCard member benefits, it'll be easier to modify the 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
int calculateCharge(int minutesparked, int day, char jcard)
{
	const int freehours = 1;  // hours free
	const int maxweekday = 5;  // max weekday charge
	const int maxweekend = 10; // max weekend/holiday charge
	const int weekdayhourlyrate = 1; // weekday rate
	const int weekendhourlyrate = 2;  // weekend rate
	const int jcardallowedhours = 6; // JCard hours before max
	const int nonjcardallowedhours = 4; // nonJCard hours before max
	int hourscharged = 0; // hours beyond free hour
	int parkingcharge = 0; // rate to be calculated and returned by function
	
	//find number of hours over free hours
	//round up partial hours with ceil (in <cmath>)
	if (minutesparked > freehours*60)
		hourscharged = ceil((minutesparked-60)/60.0);
	
	//calculate parking charges
	if (jcard == 'y' || jcard == 'Y')
	{
		//weekday parking
		if (day==1 || day==2 || day==3 || day==4 || day==5)
		{
			if (hourscharged == 0)
				parkingcharge = 0;
			else if (hourscharged > jcardallowedhours)
				parkingcharge = maxweekday;
			else 
				parkingcharge = hourscharged * weekdayhourlyrate;
		}
		else//weekend parking
		{
			if (hourscharged == 0)
				parkingcharge = 0;
			else if (hourscharged > jcardallowedhours)
				parkingcharge = maxweekend;
			else 
				parkingcharge = hourscharged * weekendhourlyrate;
		}
	}
	else //not a jcard holder
	{
	//code here
	}
	return parkingcharge; // return calculated charge to main
}
For the balance - I think I'd validate the amount entered when they enter it in Main. While the pay_money is less than the total, reprompt them to enter it.

Then in the function you can just return the difference between the money they paid and the amount they owed. No need for a local variable in the function.

1
2
3
4
int calculateChange(int charge, int payment)
{
	return payment - charge;
}



Edit: or one of the alternatives AbstractionAnon posted above
Last edited on
Omg! Cant believe how active members of this forum! Haha! Thx guys!! Really thx a lot for helping me improve my coding! Appreciate u guys effort to help me~ Will update my new code and if any problems come out, please help me out too :)
Tq
So its now done. But the value of the J card user that spent 7 hours in aeon is more expensive than non J card user, which is abnormal. I will try look into it. Thx a lot :)
Last edited on
Topic archived. No new replies allowed.