Clear Filters
Clear Filters

User input, for loop printing

2 views (last 30 days)
Cameron
Cameron on 17 Nov 2014
Answered: Julia on 17 Nov 2014
How do I make the for loops add my values to user_dia_prcnt and display it? The current code will only print out 0
clear all
clear clf
clear clc
user_dia_prcnt = 0;
user_age = input('Type your age: ');
user_gender = input('Type M or F: ','s');
%Gathers BMI
user_weight = input('Type your weight (lbs): ');
user_height = input('Type your height (in): ');
user_bmi = (user_weight*703)/user_height^2;
%add fprintf to final summary
fprintf('Your Body Mass Index is %f\n', user_bmi);
if(user_gender == 'M')
if(user_age <= 45)
if(user_bmi >= 35)
user_dia_prcnt + 66.1;
if(user_bmi <= 35)
user_dia_prcnt + 51.8;
if(user_bmi <= 30)
user_dia_prcnt + 25.5;
if(user_bmi <= 25)
user_dia_prcnt + 16.9;
else
user_dia_prcnt + 6.2;
end
end
end
end
end
end
fprintf('Percentage is: %f \n ',user_dia_prcnt);

Answers (1)

Julia
Julia on 17 Nov 2014
Hi,
this code works:
clear all
clear clf
clear clc
user_dia_prcnt = 0;
user_age = input('Type your age: ');
user_gender = input('Type M or F: ','s');
%Gathers BMI
user_weight = input('Type your weight (lbs): ');
user_height = input('Type your height (in): ');
user_bmi = (user_weight*703)/user_height^2;
%add fprintf to final summary
fprintf('Your Body Mass Index is %f\n', user_bmi);
if strcmp(user_gender, 'M')
if(user_age <= 45)
if(user_bmi >= 35)
user_dia_prcnt = user_dia_prcnt + 66.1;
elseif(user_bmi <= 25)
user_dia_prcnt = user_dia_prcnt + 51.8;
elseif(user_bmi <= 30)
user_dia_prcnt = user_dia_prcnt + 25.5;
elseif(user_bmi <= 35)
user_dia_prcnt = user_dia_prcnt + 16.9;
else
user_dia_prcnt = user_dia_prcnt + 6.2;
end
end
end
fprintf('Percentage is: %f \n ',user_dia_prcnt);
You don't specify what happens, if the user is female. And I think other cases are missing as well.

Categories

Find more on Creating and Concatenating Matrices 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!