What is wrong with this code - if and else if statements

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

 Accepted 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

6 Comments

Thank you James. I've done both of your suggestions. It still doesn't print anything. Is there any problem with the nesting part? does that look ok?
I ran your code with my modifications several times and it does indeed print results. E.g.,
Enter the tybe of the, Sedan or SUV: SUV
Enter the number of days: 35
Enter the miles travelled: 30
The cost of the rent is 2191.40 $.
Enter the tybe of the, Sedan or SUV: sedan
Enter the number of days: 20
Enter the miles travelled: 42
The cost of the rent is 1345.78 $.
Maybe you could post your current code as a comment here and we can see what problems still remain.
This my latest code which still prints nothing. Can it be my Matlab software bug?
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
rent = (79*days);
else miles>80
rent = (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
rent = (69*days);
else miles>100
rent = (69*days)+((miles-100)*0.59);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
elseif days>=30
if miles<=120
rent = (59*days);
else miles>120
rent = (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
rent = (84*days);
else miles>80
rent = (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
rent = (74*days);
else miles>100
rent = (74*days)+((miles-100)*0.64);
fprintf('The cose of the rent is %6.2f $. \n',rent)
end
elseif days>=30
if miles<=120
rent = (64*days);
else miles>120
rent = (64*days)+((miles-120)*0.54);
fprintf('The cost of the rent is %6.2f $. \n',rent)
end
end
end
You missed the upper case conversion on this line:
switch upper(cartype)
Also, your else clauses just need to have the word "else" and nothing else (no pun intended). E.g.,
else miles>80
should be this instead
else
I got the results. Thank you for your help. I appreciate it
One other change you need to make is the location of your fprintf statements. The way you have it now, some of your cases still don't print anything since they don't have fprintf statements in them. You could put fprintf statement under each block, of course, but maybe a better approach is to just have one fprintf statement at the end of the code instead. That way if you decide to change the format of your output, you only have one code line to change instead of potentially dozens.
Also, it would be wise to include an "otherwise" case in your switch statement to catch car inputs that didn't match SEDAN or SUV.

Sign in to comment.

More Answers (0)

Categories

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!