Azzera filtri
Azzera filtri

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

1 visualizzazione (ultimi 30 giorni)
Lesly
Lesly il 21 Set 2023
Commentato: Voss il 21 Set 2023
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: ');

Risposte (1)

Voss
Voss il 21 Set 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 Commenti
Lesly
Lesly il 21 Set 2023
Error using input
Unrecognized function or variable 'A'.
Error in Ex2_6(line 5)
grade = input('Enter your letter grade: ','s');
Voss
Voss il 21 Set 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.

Accedi per commentare.

Categorie

Scopri di più su Manage Products in Help Center e File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by