No-Input: Error Checking Problem

조회 수: 3 (최근 30일)
Neshant Thiru
Neshant Thiru 2020년 4월 19일
댓글: Mehmed Saad 2020년 4월 19일
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')

채택된 답변

Mehmed Saad
Mehmed Saad 2020년 4월 19일
편집: Mehmed Saad 2020년 4월 19일
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
Mehmed Saad
Mehmed Saad 2020년 4월 19일
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 2020년 4월 19일
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

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Graphics Object Programming에 대해 자세히 알아보기

제품


릴리스

R2019b

Community Treasure Hunt

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

Start Hunting!

Translated by