# help on matlab code

I would like to display errors in case a user enters a number less or equal to 0.how can i put it? thanks

Problem:Write a script areaMenu that will print a list consisting of “cylinder”, “circle”, and “rectangle”. It prompts the user to choose one, and then prompts the user for the appropriate quantities (e.g., the radius of the circle) and then prints its area. If the user enters an invalid choice, the script simply prints an error message. The script should use a nested if-else statement to accomplish this. (units are assumed to be inches) source:attawy matlab introduction and problem solving in matlab chapter 4 problem 27

%This script asks the user for a type of area
% and prints which type of area to calculate using if-else
%units are assumed to inches
mychoice = menu('choose an area','cylinder', 'cycle','rectangle');
if mychoice == 1
len=input('Enter the length of cylinde:');
%the cylinder is closed both sides
%print area.
fprintf('The area of cylinder is %.2f\n',areacylinder)
elseif mychoice == 2
%print area
fprintf('The area of a cycle is %.2f\n',areacycle)
elseif mychoice == 3
wid=input('Enter the width of rectangle:');
len=input('Enter the length of rectangle:');
arearectangle=2*(wid+len);
%print area.
fprintf('The area of a rectangle is %.2f\n',arearectangle)
end

• welcome to MATHEMATICS. It's better that you point out your question in title. You can edit it. – Hoseyn Heydari Feb 15 '14 at 12:19
• @Hoseyn:Thanks I have edited it – joke Feb 15 '14 at 12:32

There are several ways you can achieve this. The simplest would probably be to add something like

if (rad<=0 || len<=0) then
disp('Error! Please use positive numbers only.');
return;
end;


after the user inputs the lengths (of course using rad<=0 or wid<=0 || len<=0 instead if the user chooses circle or rectangle respectively). This will exit the script if the input is negative. If you want to give the user a chance to input a positive number instead of starting over, you could do something like

rad=input('Enter the radius of cylinder:');
disp('Error! Please enter a positive number.');
end;


for each of the input variables.

• :Well get you,let me try it – joke Feb 15 '14 at 13:48
• :yap it goes well now thanks – joke Feb 15 '14 at 14:00

here is a good alternative as I read more and worked out!

%areaMenu
%This script asks the user for a type of area
%and prints the area using if-else
%units are assumed to be inches.
i=1;
while( i==1 || i==2 || i==3)
disp('1. Cylinder');
disp('2. Circle');
disp('3. Rectangle');
%choice selection
if i==1
%error check
while(r<=0)
disp('Error! Please enter a positive number')
end
l=input('enter length: ');
%error check
while(l<=0)
disp('Error! Please enter a positive number')
l=input('enter length again: ');
end
%area calculated and print
area=2*pi*r*l;
fprintf('The area is %.2f\n',area);
elseif i==2
%error check
while(r<=0)
disp('Error! Please enter a positive number')
end
%area calculated and print
area=2*pi*r;
fprintf('The area is %.2f\n',area);
elseif i==3
l=input('enter length: ');
%error check
while(l<=0)
disp('Error! Please enter a positive number')
l=input('enter length again: ');
end
w=input('enter width: ');
%error check
while(w<=0)
disp('Error! Please enter a positive number')
w=input('enter width again: ');
end
%area calculated and print
area=l*w;
fprintf('The area is %.2f\n',area);
else
fprintf('invalid choice! Enter a correct choice next time');
end
end