Clear Filters
Clear Filters

Why does my it keep saying error on the input line?

1 view (last 30 days)
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
Voss on 21 Sep 2023

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.
  4 Comments
Lesly
Lesly on 21 Sep 2023
Error using input
Unrecognized function or variable 'A'.
Error in Ex2_6(line 5)
grade = input('Enter your letter grade: ','s');
Voss
Voss on 21 Sep 2023
I get that same error when I don't use the 's' option and enter A, but I get no error when I use the 's' option. Strange.

Sign in to comment.

Categories

Find more on Manage Products 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!