Function not giving an output
Show older comments
The code I have is to find the cost of a parking bill. I want the code to return an error message if the inputs are not within my parameters but when I run it, there is not output. pt is a deliniation between long term and short term parking and time is suppose to be a 4 element row vector.
function y = Parking_Bill(pt,time)
if (pt == (1 || 2) && (size(time) == 4))
w = time(1);
d = time(2);
h = time(3);
m = time(4);
if pt == 1
if d>7
weeks = (d/7);
w_fixed = fix(weeks)+ w;
d_fixed = round(rem(d,7));
else
w_fixed = w;
d_fixed = d;
end
if h>24
days= (h/24);
d_fixed= fix(days);
h_fixed = rem(h,24)*24;
else
h_fixed = h;
end
if m > 60
hours = m/60;
h_fixed = fix(hours);
end
if w_fixed >= 1
weekly_bill= 50 * w_fixed;
else
weekly_bill = 0;
end
if d_fixed>=1
daily_bill = 8* d_fixed;
else
daily_bill=0;
end
if h_fixed >= 1
hourly_bill = 2+ (h_fixed-1);
else
hourly_bill = 0;
end
total_bill = weekly_bill + daily_bill + hourly_bill;
y = fprintf('Total bill =$%d.00', total_bill);
if pt == 2
if w > 0
weeks_to_minutes = (w * 10080);
end
if d > 0
days_to_minutes = (d * 1440);
end
if h > 0
hours_to_minutes = (h * 60);
end
if m > 30
minutes = (m/20);
m_fixed= fix(minutes) * 2;
else
m_fixed = 0;
end
total_bill = weeks_to_minutes + days_to_minutes + hours_to_minutes + m_fixed;
y = fprintf('Total bill =$%d.00', total_bill);
else
y = fprintf('Invaild input, check input');
return;
end
end
end
end
3 Comments
KSSV
on 10 Apr 2022
What inputs you have tried?
Eric Brown
on 10 Apr 2022
the cyclist
on 10 Apr 2022
Can you give specific examples of valid and invalid inputs?
Accepted Answer
More Answers (1)
the cyclist
on 10 Apr 2022
Edited: the cyclist
on 10 Apr 2022
This condition almost certainly does not do what you expect:
(pt == (1 || 2) && (size(time) == 4))
The problem here is (at least) two-fold. The expression
(1||2)
is going to just evaluate to true. Then you are asking if
pt == true
which presumably is not what you mean. I think you want
(pt==1)||(pt==2)
Also,
size(time)
is always going to be a two-element vector -- row count and column count, so you are going to get a two-element vector from that expression.
Categories
Find more on App Building in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!