MATLAB: What is wrong with this code – if and else if statements

if statementnesting

Hi all, I'm new to Matlab and programming. I'm trying to solve a question but when I run the script it asks me for the values and when I enter them it doesn't print anything. Can anyone please tell me what is the problem?
clc
clear all
car = input('Enter the tybe of the, Sedan or SUV: ','s');
days = input('Enter the number of days: ');
miles = input('Enter the miles travelled: ');
cartype = char(car);
switch cartype
case char('Sedan')
if days>=1 && days<=6
if miles<=80
rent1 = (79*days)+((miles-80)*0.69);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
elseif days>=7 && days<=29
if miles<=100
rent2 = (69*days)+((miles-100)*0.59);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
elseif days>=30
if miles<=120
rent3 = (59*days)+((miles-120)*0.49);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
end
case char('SUV')
if days>=1 && days<=6
if miles<=80
rent4 = (84*days)+((miles-80)*0.74);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
elseif days>=7 && days<=29
if miles<=100
rent5 = (74*days)+((miles-100)*0.64);
fprintf('The cose of the rent is %6.2f $. \n',rent)
end
elseif days>=30
if miles<=120
rent6 = (64*days)+((miles-120)*0.54);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
end
end

Best Answer

Change all of your rent1, rent2, etc variables to just rent, since that is what you print.
Also, to make things simpler on the user, maybe make your code case insensitive. E.g.,
switch upper(cartype)
case char('SEDAN')
In addition, your code doesn't print anything if the miles driven is too high. E.g., you should add else clauses like in the following:
if miles<=80
rent1 = (79*days)+((miles-80)*0.69);
fprintf('The cost of the rent is %6.2f $. \n',rent)
else
% ADD CODE HERE TO HANDLE HIGH MILEAGE CASES
end