0

I'm trying to make a program that asks a yes or no question. Based on that answer the program will continue or terminate. I keep getting an error since the arrays don't have the same dimension. I tried to use strcmp() but failed. I don't understand how true or false will help me discriminate between different words and capitalization. (Do I have to test each letter?) I want the program to continue if the input is any of these words 'yes','YES','Yes','y' and quit if the input is 'no','NO','No','n' I really want to understand, the == feels wrong somehow. Thank You

ZZ=input('Do you want to know when you''ll turn 100?: ', 's');
NN={'no','NO','No','n'}
YY={'yes','YES','Yes','y'}

XX=strcmp(ZZ(NN),ZZ(YY)); %I thought this line would let MATLAB know everything is ok

if ZZ=='no' || ZZ=='NO' || ZZ=='No' || ZZ=='nO' || ZZ=='n'
 disp('Thank You.') 
 disp('Come again.')

elseif ZZ=='yes' || ZZ=='YES'|| ZZ=='Yes'|| ZZ=='y'

 x=input('Enter your age: '); %x is your age.
 .....
3
  • Just check the first letter, if 'n', then no, if 'y' then yes. You can use case insensitive with strcmpi. Commented Oct 13, 2017 at 18:50
  • Another fun option is questdlg. It opens a customizable window that you can click the Yes or No button. Commented Oct 13, 2017 at 18:55
  • Thanks for the suggestion. I want to use this concept for larger words. Is this a good idea? Commented Oct 13, 2017 at 22:30

1 Answer 1

1

I think if you need your program to run more than once, you need a for or while loop.

zz = 'yes';
while strcmpi(zz(1),'y')       
    x = input('Enter your age: ');   
    zz = input('Do you want to know when you''ll turn 100?: ', 's'); 
end
Sign up to request clarification or add additional context in comments.

5 Comments

You don't handle the ZZ = y case. strcmpi('y','yes') == 0
The assumption is that the user enters a yes or no.
The problem statement is pretty explicit: I want the program to continue if the input is any of these words 'yes','YES','Yes','y'
Does this while loop compare the first element of the array (zz) with the letter 'y'? Since this is true the loop runs. Does this mean that I can more letters to 'strcmpi' ? (say strcmpi(zz(n),'n', 'N','o','y','Y',.....)
Well, anything that doesn't start with 'y' will make the condition false and the program will not run. If you are asking to include multiple true conditions, then there are other ways to do it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.