Why does my it keep saying error on the input line?
Mostrar comentarios más antiguos
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: ');
Respuestas (1)
Voss
el 21 de Sept. de 2023
0 votos
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 comentarios
Lesly
el 21 de Sept. de 2023
Voss
el 21 de Sept. de 2023
Please copy and paste the entire error message (all the red text).
Lesly
el 21 de Sept. de 2023
Voss
el 21 de Sept. de 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.
Categorías
Más información sobre Debugging and Improving Code en Centro de ayuda y File Exchange.
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!