How can I keep asking the user for correct input even after they press enter?

Hello,
I want my statement to continue to be prompted until correct input is given.
Prompt the user to write "ready" or "Ready" and keep prompting the user until they do so, and if they just pressed enter, a statement shows up asking them to "Please type "Ready"". I am a little lost as to how to do that using while(1) loop. I have this so far...
while (1)
Ready= input('When you are ready to play please type "Ready": ','s');
if Ready== 'ready' | Ready== 'Ready'
break
end
end
Please help
Thank you in advance.

 Accepted Answer

Most of the time case sensitivity shouldn't be a constraint in these types of queries. The demo below accepts any form of "ready" (READY, ready, Ready, reaDY, etc).
response = '';
while ~strcmpi(response,'ready')
response = input('When you are ready to play please type "Ready": ','s');
end
If you truly only want to accept Ready and ready, use this condition instead,
while ~any(strcmp(response, {'Ready','ready'}))
Another form that gives the user feedback on their error,
instructions = 'When you are ready to play please type "Ready": ';
baseInstructions = instructions;
response = '';
while ~strcmpi(response,'ready')
response = input(instructions,'s');
instructions = sprintf('%s, (you typed "%s") ',baseInstructions, response);
end
Another idea since this is a binary task (the user types the correct word or they do not), why not use a question or message box?
qdh = questdlg('When you are ready to play, please press go!', 'Game', 'Go!','Go!');
You could also add a "Cancel" option.

5 Comments

Thank you so much for lending me your time to answer my question, but I have to stick to the material in my book as per the professor's orders, so I can't use strcmpi or the message box, but I will keep your suggestions in my mind for future references. However, I can use isempty(), the only problem is that I am not very familiar with it.
Best Regards,
JK
Had I known this was an assignment I wouldn't have given such a complete answer.
I find it alarming that the book would not instruct users to use strmp/strcmpi for string comparison. Even though the equality operator (==) works with strings, I'd strongly advise not to use that form of string comparison.
isempty() will only test if the response is empty or not.
Don't worry It is more of a challenge than an assignment. The tittle of our book is as following "MATLAB An Introduction with Applications, 6th Edition An Introduction with Applications" by Amos Gilat, so I guess that is why we don't have these functions, either that or that they mentioned them at the end, which I haven't reached yet.
I found a copy of the 5th ed of that book and strcmp or strcmpi where not mentioned in any of its 418 pages. I see the author is use R2013a which is really old (maybe the 6th ed has updates). strcmp/strcmpi are the go-to functions for string comparison. ismember is also often used along with lower() or upper() to allow for case insensitivity. But the equality test using "==" is definitely not recommended when comparing character arrays. For strings I guess it's OK but strings weren't even around in R2013a and still, the other functions I mentioned are better. isequal is another option but still not better than strcmp, strcmpi, or ismember.
Your help is very much appreciated! Thank you for pointing this out and sharing this information, will keep it in my mind.
Best Regards,
Jk

Sign in to comment.

More Answers (1)

Use
strcmpi( Ready, 'ready' )
instead of
Ready== 'ready' | Ready== 'Ready'
Don't use == in combination with character arrays to compare words. It compares character by character.
>> 'abc'=='a*c'
ans =
1×3 logical array
1 0 1
in contrary to strings
>> "abc"=="a*c"
ans =
logical
0

2 Comments

"so I can't use strcmpi" What other function are excluded?
What about this one? Replace
if Ready== 'ready' | Ready== 'Ready'
by
if string(Ready)=="ready" || string(Ready)=="Ready"
Yes this worked! Thank you very much for sharing this information!
Best Regards,
JK

Sign in to comment.

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Asked:

on 29 Jan 2021

Edited:

on 2 Feb 2021

Community Treasure Hunt

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

Start Hunting!