Why does my it keep saying error on the input line?
Show older comments
This is my code
Exercise 2.6
clear;
clc;
%Get letter grade from user
grade = input('Enter your letter grade: ');
switch grade
case{A, a}
grade_letter = 'Excellent';
case{B, b}
grade_letter= 'Good';
case{C,c}
grade_letter= 'Ok';
case{D, d}
grade_letter= 'Below Average';
case{F, f}
grade_letter= 'Fail';
otherwise
grade_letter= 'Unknown';
end
disp(grade_letter)
and this is what im getting
Error in Ex2_6 (line 5)
grade = input('Enter your letter grade: ');
Answers (1)
Voss
on 21 Sep 2023
0 votes
You should use the 's' option in input() to avoid interpreting the input, e.g., input A will be stored as the letter A rather than MATLAB looking for a variable called A.
grade = input('Enter your letter grade: ','s');
And you will also need quotes around the letters in all your case statements, e.g.:
switch grade
case {'A','a'}
% etc.
Or you could switch on upper(grade) or lower(grade) to only have to include one letter case in each case statement:
switch lower(grade)
case 'a'
% etc.
Categories
Find more on Clocks and Timers 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!