I am trying to complete a while loop that uses letters instead of numbers as the accepted input. The accepted inputs are M,m,F,f. How do I prevent other inputs from getting through?
2 views (last 30 days)
Show older comments
% (calculating the Ideal Body Weight (IBW))
ideal_body_weight_male_kg = 50.0 + (2.3 * (height_inches - 60));
ideal_body_weight_female_kg = 45.5 + (2.3 * (height_inches - 60));
% (input for gender function)
ideal_body_weight = input('Enter the gender (M/m or F/f): ', 's');
% (while loop to validate input)
while ideal_body_weight;
That is what I need help with. The following is an example of other while loops I have used in this program that work.
%( user inputs height in inches)
height_inches = input('Enter height in inches (59-78): ');
% (while loop to validate input)
while height_inches;
if height_inches > 78
height_inches = input('Enter height in inches (59-78): ');
elseif height_inches < 59;
height_inches = input('Enter height in inches (59-78): ');
else
break
end
end
1 Comment
Accepted Answer
Azzi Abdelmalek
on 27 Feb 2013
Edited: Azzi Abdelmalek
on 27 Feb 2013
EDIT
s=input('genre','s')
genre='FfMm'
while ismember(s(1),genre) % s is the input
5 Comments
James
on 1 Mar 2013
Edited: Azzi Abdelmalek
on 1 Mar 2013
user_Gender = input( 'Is the person a female or male? Enter F or M: ', 's' );
while user_Gender ~= 'M , m || F , f'
user_Gender = input ( 'Is the person a female or male? Enter F or M: ', 's' );
end
Azzi Abdelmalek
on 1 Mar 2013
user_Gender = input( 'Is the person a female or male? Enter F or M: ', 's' );
while ~ismember(user_Gender,{'M' ,'m','F' ,'f'})
user_Gender = input ( 'Is the person a female or male? Enter F or M: ', 's' );
end
More Answers (0)
See Also
Categories
Find more on Loops and Conditional Statements in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!