No-Input: Error Checking Problem
2 次查看(过去 30 天)
显示 更早的评论
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')
0 个评论
采纳的回答
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
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
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 Center 和 File Exchange 中查找有关 Graphics Object Programming 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!