How can I repeat question until valid input is entered?

I have a bit of code that asks two questions. I want the questions to continue to be prompted until a valid response is given. For the first question, they need to enter in any number greater than zero. For the second question they must enter in A, B, C, D, or E. Any other response should either repeat the question or potentially rephrase the question to emphasize the correct format. Here is the code that works as long as they enter the response correctly.
%Input
fpc = input('Enter specified compressive strength (psi): ');
Facility = input('Chooose facility letter in upper-case (A-E): ', 's');
And just to show one of the things I've tried:
while Facility ~= 'A,B,C,D,E';
Facility = input('Chooose facility letter in upper-case (A-E): ', 's');
end
And this actually does work, unless the user enters in a value that is more than one character long, then I get a 'matrix dimensions must agree', which doesn't make sense to me. In any case, I'm sure there is a better way to do this.
Thank you

 Accepted Answer

What if you implement it using a while loop?
%Input
clc;
clear all;
close all;
fpc = input('Enter specified compressive strength (psi): ');
coffee = 0;
while ~coffee
Facility = input('Chooose facility letter in upper-case (A-E): ', 's');
if ~ismember(Facility, {'A','B','C','D','E'})
coffee = 0;
else coffee = 1;
end
end
This will guarantee that the Facility will take a character from A-E, and if not, will continue to loop.

5 Comments

I tried to use a while loop as indicated in my post, but I will try yours. In principle I understand how while loops work, but I have very little experience with them (or matlab and programming for that matter). Could you explain what your code is going to do? Specifically the tilde portion.
while ~coffee
I understand that while loops executes a statement until it is false, but I suppose I don't understand the syntax.
And just in case it's important here is the next snippet of code.
% Develop all data files for all facilities
if Facility == 'A'
myfacdata = textread ('FacA.txt');
elseif Facility == 'B'
myfacdata = textread ('FacB.txt');
elseif Facility == 'C'
myfacdata = textread ('FacC.txt');
elseif Facility == 'D'
myfacdata = textread ('FacD.txt');
elseif Facility == 'E'
myfacdata = textread ('FacE.txt');
end
ntests = length(myfacdata);
Now you can see why I can't have anything besides A, B, C, D, or E.
Thank you for taking the time to respond.
OK, I tried your solution and it does loop, but I'm still getting the matrix dimensions must agree error when I enter an answer longer than one character.
Chooose facility letter in upper-case (A-E): 9j
Error using ~=
Matrix dimensions must agree.
Error in concprojprogress (line 30)
if Facility ~= 'A,B,C,D,E';
I made an error in the last code, but I have updated it just now.
coffee = 0;
while ~coffee
First we are defining a variable called coffee and setting its variable to zero. Then the next line says that while coffee is not true, which makes the while statement true; hence the while loop starts.
Now after the loop starts, we are asking for an input from the user.
Facility = input('Chooose facility letter in upper-case (A-E): ', 's');
After we get the input, if statement starts.
if ~ismember(Facility, {'A','B','C','D','E'})
coffee = 0;
else coffee = 1;
end
The if statement states that if the variable Facility that saves the input of the user does not match (~) with one of the strings from A-E, the coffee variable is set to zero again. Since the coffee is now zero again, the while loop starts all over again.
The only escape condition from the while loop is to set coffee equal to one, i.e., the Facility variable must match one from A-E. Until then, the while loop will keep on going.
Thank you so much. It works perfectly.

Sign in to comment.

More Answers (0)

Categories

Find more on MATLAB in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!