If-Else Statement Error

code = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
if code==25
fprintf ('Correct code! /n')
else
fprintf ('please try again /n')
code = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
end
for some reason, i keep getting this error (Operator '==' is not supported for operands of type 'cell'. Error in EscapeRoom (line 2)
if code==25) whenever I input a value into the pop-up window and I've tried several functions and it still wont work, what do I have to change in my code to get it to work?

Answers (1)

The inputdlg returns a cell array of the string that the user enters. You need to convert that to a numeric data type. Here is one way:
codeCell = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
code = str2double(codeCell);
if code==25
fprintf ('Correct code! /n')
else
fprintf ('please try again /n')
code = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
end

5 Comments

Perfect, I tried that and it worked. But I want it to loop until the correct code has been inputted and now the please try again window only pops up once, even when they put an incorrect value it just stops it doesn't loop. This is my code right now, I just updated a few things:
codeCell = inputdlg('Please enter the Aplphabet Block Puzzle code:');
code = str2double(codeCell);
if code==892
fprintf ('Correct code!')
else
code = inputdlg('Incorrect code, Please try again:');
end
also, could we change the numberical input into a string, we want to use ABCD instead of 892
Your original code never looped. I would do that with a while loop.
I changed the code so that it doesn't convert to numeric, but uses strcmp to check the string instead. It will work for 'ABCD'.
codeCell = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
code = cell2mat(codeCell);
while ~strcmp(code,'25')
disp('please try again')
codeCell = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
code = cell2mat(codeCell);
end
disp('Correct code!')
I also changed your fprintf to disp instead, which I think is a simpler way to do what you were trying to do.
Thank you!!
Also I have two "puzzles" I want to include in this code, so once the first puzzle has been answered correctly it moves on to the next one, but whenever it does that and I try putting in the correct code for the second puzzle, I get this error message: (Error using str2double. Too many input arguments.Error in EscapeRoom (line 11). code = str2double(code,'2581');)
This is my code right now:
codeCell = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
code = cell2mat(codeCell);
while ~strcmp(code,'ABCD')
disp('please try again')
codeCell = inputdlg('Please enter the Alphabet Blocks Puzzle code:');
code = cell2mat(codeCell);
end
disp('Correct code!')
codeCell = inputdlg('Please enter the Arduino Puzzle code:');
code = str2double(code,'2581');
while str2double(codeCell);
disp('please try again')
codeCell = inputdlg('Please enter the Arduino Puzzle code:');
code = str2double(codeCell);
end
disp('Correct code!')
You are being sloppy in using the code I posted. You should spend more time trying to understand the functions I used, instead of just blindly copy/paste.
Specifically, you are mixing up how I used strcmp and how I used str2double. I would look only at the latest code I posted -- which did not use str2double at all -- and try again to make that work for your second code.
I just figured it out and it works perfectly, thank you for the help.

Sign in to comment.

Categories

Find more on Strategy & Logic in Help Center and File Exchange

Asked:

on 15 Mar 2021

Commented:

on 15 Mar 2021

Community Treasure Hunt

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

Start Hunting!