Help switch case problem

15 views (last 30 days)
THANH NGUYEN
THANH NGUYEN on 13 Feb 2013
The requirement of this assignment is to calculate the Ideal Body Weight (IBW). The following equations are used to compute the IBW, where height is in inches and the IBW is in kilograms. The user will need to specify the gender by entering a character notation, like M/m or F/f.
What's wrong with my code? I can't run it.
gender=input('Enter the gender (F/f or M/m):');
ibwWeight=IBW*2.2046 % pounds
switch(gender)
case {'F', 'f'}
IBW = 45.5 + 2.3 * (Height-60);
case {'M', 'm'}
IBW = 50.0 + 2.3 * (Height-60);
end
fprintf('The IBW is: %.1f pounds\n',ibwWeight)
  1 Comment
Jan
Jan on 13 Feb 2013
Please explain more details of "I can't run it". Do you get an error message, do the results differ from your expectations, do you have Matlab installed?

Sign in to comment.

Answers (2)

Azzi Abdelmalek
Azzi Abdelmalek on 13 Feb 2013
gender=input('Enter the gender (F/f or M/m):','s');
Height=100
switch(gender)
case {'F', 'f'}
IBW = 45.5 + 2.3 * (Height-60);
case {'M', 'm'}
IBW = 50.0 + 2.3 * (Height-60);
end
ibwWeight=IBW*2.2046
fprintf('The IBW is: %.1f pounds\n',ibwWeight)
  4 Comments
James
James on 14 Feb 2013
I get an error when I run my program like this ^^^ The error is
Error using input Undefined function or variable 'M'.
Error in Lab (line 84) gender = input( 'Enter the gender: ')
I've been fiddling with it for a while and have gotten nowhere
Azzi Abdelmalek
Azzi Abdelmalek on 14 Feb 2013
Edited: Azzi Abdelmalek on 14 Feb 2013
If you use
gender=input('Enter the gender (F/f or M/m):');
You are expecting a string 'f/m' or 'F/M' as an input. then when you enter for example F, there will be an error. To allow entering F you to add 's' as an option in:
gender=input('Enter the gender (F/f or M/m):','s');
I don't know if you've read my answer.

Sign in to comment.


THANH NGUYEN
THANH NGUYEN on 13 Feb 2013
Edited: THANH NGUYEN on 13 Feb 2013
the target weight for target bmiw. I don't know why I got the wrong pound. It should be 184.3 pounds for bmi=25, but i got 165 pounds. The third can't run due to some error which I dont know
% Prompt the user to enter a value for weight in pounds and height in inches.
Weight=input('Enter the weight in pounds:');
Height=input('Enter the height in inches:');
%Compute conversion
kiloWeight=Weight/2.2046; %kg
meterHeight=Height*2.54/100; %meters
disp(' ')
%Display BMI classification
bmi=kiloWeight/meterHeight^2;
fprintf('The BMI is: %.2f\n',bmi)
if bmi<18.5
disp('BMI classification:underweight');
elseif 18.5 <=bmi & bmi < 24.9
disp('BMI classification:Normal weight');
elseif 25 <=bmi & BMI < 29.9
disp('BMI classification:Overweight');
else
disp('BMI classification: Obese');
end
disp(' ')
% Target BMI and Weight
bmiTarget=input('Enter the target BMI:');
% Compute target weight
kilotargetWeight=bmi*meterHeight^2; %kg
%Compute Conversion
poundtargetWeight=kilotargetWeight*2.2046;
fprintf('The target weight is: %.1f pounds\n',poundtargetWeight)
disp(' ')
gender=input('Enter the gender (F/f or M/m):','s');
switch(gender)
case {'F', 'f'}
IBW = 45.5 + 2.3 * (Height-60);
case {'M', 'm'}
IBW = 50.0 + 2.3 * (Height-60);
end
fprintf('The IBW is: %.1f pounds\n',ibwWeight)

Community Treasure Hunt

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

Start Hunting!