i have a code that isnt running through all the if statements

1 view (last 30 days)
Riley Phillips
Riley Phillips on 3 May 2022
Edited: Stephen23 on 3 May 2022
this is the very first part of my code
replyOK = false;
SkaDan = 'are we Skating or dancing today?\n';
while ~replyOK
reply = input(SkaDan, 's');
if strcmpi(reply, 'skating') || strcmpi(reply, 'dancing')
replyOK = true; % or "break" to break out of loop
end
end
if ('skating') %<SM IF>
fprintf('skating awsome!\n')
replyOK = false;
FootType = 'what is the shape of your foot? skinny, wide, or normal\n';
while ~replyOK
reply = input(FootType, 's');
if strcmpi(reply, 'skinny') || strcmpi(reply, 'wide')|| strcmpi(reply, 'normal')
replyOK = true; % or "break" to break out of loop
end
end
elseif('dancing')
replyOK = false;
FootType = 'what is the shape of your foot? skinny, wide, or normal\n';
while ~replyOK
reply = input(FootType, 's');
if strcmpi(reply, 'skinny') || strcmpi(reply, 'wide')|| strcmpi(reply, 'normal')
replyOK = true; % or "break" to break out of loop
end
end
but the problem is, no matter what you choose it always out puts skating instead of dance or what ever i pick, can some one please tell me how to fix this. i still need it to accept it as a word and not a number

Answers (1)

Stephen23
Stephen23 on 3 May 2022
Edited: Stephen23 on 3 May 2022
The problem is how you have used IF and ELSIF. All character codes in the text 'skating' are all non-zero, so
if ('skating')
that condition is considered to be TRUE and so the code inside that IF statement is evaluated. You thought that you were comparing the text 'skating' with what the user input, but where do you compare with the user input? You just told MATLAB to use the literal text 'skating' and so that is what it does. Replace that buggy line with
if strcmpi(SkaDan,'skating')
and similarly for the ELSEIF.
Note that you can get rid of those IFs inside the WHILE loops simply with this:
replyOK = strcmpi(..) || strcmpi(..) ..

Categories

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!