Hi, I'm having trouble getting my script to prompt the user to re-enter the input if it isn't a number. I've seen a few similar questions but none seem to work for me.

2 次查看(过去 30 天)
i = input('Please enter which you are converting from, ''imperial'' or ''metric'' : ', 's');
i = lower(i);
switch i
case 'imperial'
gallons = input('Please enter measurement in Gallons: ');
litres = (gallons*3.78544);
str = ['The value is equal to ' num2str(litres) ' Litres'];
disp(str)
case 'metric'
litres = input('please enter measurement in Litres: ');
gallons = (litres/3.78544);
str = ['The value is equal to ' num2str(gallons) ' Gallons'];
disp(str)
otherwise
str = ['That input is invalid.'];
disp(str)
VolConv;
end
%it works fine when I call for the VolConv.m file to run again, but in the case the user enters letters when asked for the measurement, I get errors. It does prompt them again until they enter a number, but I want to eliminate the red error message for one of my own and then prompt again. Thanks in advance!

回答(2 个)

Stephen23
Stephen23 2017-4-6
编辑:Stephen23 2017-4-7
Always use the 's' option with input, even when you want a numeric value, e.g.:
gallons = str2double(input('Please enter measurement in Gallons: ','s'));
This will prevent bad things from happening if the user inputs valid code (either accidentally or maliciously), and will output NaN for any invalid inputs (i.e. anything that is not a number). You can test for the NaN using isnan.
  3 个评论
James Price
James Price 2017-4-21
Ahh, Cool I'll try that one out. Apparently it's something that we don't need to address at this stage of our course, but I'll definitely play around with it once I've got the assigment finished Thanks!

请先登录,再进行评论。


Image Analyst
Image Analyst 2017-4-6
See this snippet. Put it inside a loop that calls it until your users enter the correct information:
% Ask user for two floating point numbers.
defaultValue = {'45.67', '78.91'};
titleBar = 'Enter a value';
userPrompt = {'Enter floating point number 1 : ', 'Enter floating point number 2: '};
caUserInput = inputdlg(userPrompt, titleBar, 1, defaultValue);
if isempty(caUserInput),return,end; % Bail out if they clicked Cancel.
% Convert to floating point from string.
usersValue1 = str2double(caUserInput{1})
usersValue2 = str2double(caUserInput{2})
% Check usersValue1 for validity.
if isnan(usersValue1)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue1.
usersValue1 = str2double(defaultValue{1});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue1);
uiwait(warndlg(message));
end
% Do the same for usersValue2
% Check usersValue2 for validity.
if isnan(usersValue2)
% They didn't enter a number.
% They clicked Cancel, or entered a character, symbols, or something else not allowed.
% Convert the default from a string and stick that into usersValue2.
usersValue2 = str2double(defaultValue{2});
message = sprintf('I said it had to be a number.\nTry replacing the user.\nI will use %.2f and continue.', usersValue2);
uiwait(warndlg(message));
end
  2 个评论
James Price
James Price 2017-4-21
Thanks for your reply! We're not up to GUIs for this stage of our assignment, but the next stage does, so could do the job then. Thanks again!
Image Analyst
Image Analyst 2017-4-21
You can use input() instead of inputdlg() if you're not allowed to use dialog boxes yet. input() is a more primitive command window-based function.

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Programming 的更多信息

Community Treasure Hunt

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

Start Hunting!

Translated by