No-Input: Error Checking Problem

3 vues (au cours des 30 derniers jours)
Neshant Thiru
Neshant Thiru le 19 Avr 2020
Commenté : Mehmed Saad le 19 Avr 2020
clear, clc
disp('Welcome to Basic Conversion Script');
prompt = 'Please input t for Temperature, l for Length, or m for Mass: ';
z = input (prompt, 's');
w = isempty(z);
if w == 0
disp('Error! There is not input');
end
while ~(z == 't' || z == 'l' || z == 'm')
z = input ('Error! Please input wanted values again: ', 's');
w = isempty(z);
if w == 0
disp('Error! There is not input');
end
end
if strcmp(z,'t')
disp('1. To Convert Celsius to Fahrenheit' );
disp('--');
disp('2. To Convert Fahrenheit to Celsius ');
x = input('Please choose a number betwenn 1 or 2: ');
w = isempty(x);
if w == 0
disp('Error! There is not input');
end
while ~(x == 1 || x == 2)
if strcmp(x, 's')
x = input ('Error! Please choose a number betwenn 1 or 2 again: ');
elseif (x < 0) %This if-statement is error checking for any negative values
x = input ('Error! Please choose a number betwenn 1 or 2 again: ');
else
x = input ('Error! Please choose a number betwenn 1 or 2 again: ');
end
end
if x == 1
C = input('Enter the Temperatue in Celsius: ');
w = isempty(C);
if w == 1
disp('Please Input a number to convert next time');
end
F = (C * 9/5) + 32;
fprintf('The Temperature in Fahrenheit is: %1.2f\n', F );
fprintf(1, '\n');
end
if x==2
F = input('Enter the Temperatue in Fahrenheit: ');
w = isempty(F);
if w == 1
disp('Please Input a number to convert next time');
end
C = (F - 32) * 5/9;
fprintf('The Temperature in Celcius is: %1.2f\n', C );
fprintf(1, '\n');
end
end
So I'm currently creating a conversion script. And I'm currently trying to do a error check that makes sure it sends out an a error message when the user doesn't input anything.
The error I get is:
Operands to the || and && operators must be convertible to logical scalar values.
Error in Milestone2 (line 11)
while ~(z == 't' || z == 'l' || z == 'm')

Réponse acceptée

Mehmed Saad
Mehmed Saad le 19 Avr 2020
Modifié(e) : Mehmed Saad le 19 Avr 2020
Replace your while loop conditions
you are comparing z with a single character suppose someone enters
z = 'abdft'
the output of z=='t' will be [0 0 0 0 1] and you dont want that
Use strcmp (check its help)
while ~(strcmp(z,'t')||strcmp(z,'l')||strcmp(z,'m'))
Also you need to reverse this condition
w = isempty(z);
if w == 0
disp('Error! There is not input');
end
because whenever w is empty it will give you 1 while your condition is if w ==0 display error
but error actually comes when w==1
either change
w = ~isempty(z)
or change
if w==1
  3 commentaires
Mehmed Saad
Mehmed Saad le 19 Avr 2020
it is working for me
clear, clc
disp('Welcome to Basic Conversion Script');
prompt = 'Please input t for Temperature, l for Length, or m for Mass: ';
z = input (prompt, 's');
w = ~isempty(z);
if w == 0
disp('Error! There is not input');
end
while ~(strcmp(z,'t')||strcmp(z,'l')||strcmp(z,'m'))
z = input ('Error! Please input wanted values again: ', 's');
w = ~isempty(z);
if w == 0
disp('Error! There is not input');
end
end
if strcmp(z,'t')
disp('1. To Convert Celsius to Fahrenheit' );
disp('--');
disp('2. To Convert Fahrenheit to Celsius ');
x = input('Please choose a number betwenn 1 or 2: ');
w = ~isempty(x);
if w == 0
disp('Error! There is not input');
end
while ~(x == 1 || x == 2)
if strcmp(x, 's')
x = input ('Error! Please choose a number betwenn 1 or 2 again: ');
elseif (x < 0) %This if-statement is error checking for any negative values
x = input ('Error! Please choose a number betwenn 1 or 2 again: ');
else
x = input ('Error! Please choose a number betwenn 1 or 2 again: ');
end
end
if x == 1
C = input('Enter the Temperatue in Celsius: ');
w = isempty(C);
if w == 1
disp('Please Input a number to convert next time');
end
F = (C * 9/5) + 32;
fprintf('The Temperature in Fahrenheit is: %1.2f\n', F );
fprintf(1, '\n');
end
if x==2
F = input('Enter the Temperatue in Fahrenheit: ');
w = isempty(F);
if w == 1
disp('Please Input a number to convert next time');
end
C = (F - 32) * 5/9;
fprintf('The Temperature in Celcius is: %1.2f\n', C );
fprintf(1, '\n');
end
end
Mehmed Saad
Mehmed Saad le 19 Avr 2020
the only thing is
x = input('Please choose a number betwenn 1 or 2: ');
if user put a string an error will occur because input is expecting to get numerical input

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

En savoir plus sur Graphics Object Programming dans Help Center et File Exchange

Produits


Version

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by