MATLAB Answers

0

How to write a Questions that requires Yes or No asnwer

Asked by Michael Pipicelli on 5 Sep 2019
Latest activity Commented on by Rena Berman on 19 Sep 2019
Hi,
I am trying to write a code where the user is ask a Yes or No questions and if they answer Yes the program conitunes if they answer No it ends
So far this is what i have
% Get building id
building_id = input('Enter the building ID >', 's');
wall = input('Enter a Wall (Y/N)>','s');
while wall == Y
continue
wall == N
quit
end

  6 Comments

I agree with Rik. Many times there are values to understand how a question or answer starts from its initial stage and becomes clear. Deleting the head makes the rest clueless and more confusing.
menu() only permits the user to cancel or choose one of the provided choices, so it becomes unnecessary to loop back until the user chooses Y or N because they cannot choose anything else.
(Answers Dev) Restored edit

Sign in to comment.

3 Answers

Answer by Fangjun Jiang on 5 Sep 2019

wall ='Y'
while wall == 'Y'
building_id = input('Enter the building ID >', 's');
wall = input('Enter a Wall (Y/N)>','s');
end

  3 Comments

The user needs to input an answer of Y or N;
if it is N the code stop and calculator the answer ir the answer
if Y the user moves onto the next steps which is to calculate wall length
In your code, there are syntax errors.
This code runs. You just need to hook up with the rest of your code.
Maybe you need to consider using if-else statement, not the while loop.

Sign in to comment.


Answer by Fangjun Jiang on 5 Sep 2019

You need to wrap the code in a function so you can use "return" to terminate the program based on the input.
function test
wall = input('Enter a Wall (Y/N)>','s');
if wall=='N'
return;
end
% Get the wall id
wall_id = input('Wall ID>');

  0 Comments

Sign in to comment.


Answer by Walter Roberson
on 5 Sep 2019

Consider using questdlg() or menu()

  0 Comments

Sign in to comment.