1
$\begingroup$

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
    disp('your choice is a cylinder')
    rad=input('Enter the radius of cylinder:');
    len=input('Enter the length of cylinde:');
     %the cylinder is closed both sides
     %print area.
    areacylinder=2*pi*rad*rad+pi*rad*len;
    fprintf('The area of cylinder is %.2f\n',areacylinder)
elseif mychoice == 2
    disp('your choice is a cycle')
    rad=input('Enter the radius of a circle:');
    areacycle=pi*rad*rad;
    %print area
    fprintf('The area of a cycle is %.2f\n',areacycle)
elseif mychoice == 3
     disp('your choice is a rectangle')
     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
$\endgroup$
  • $\begingroup$ welcome to MATHEMATICS. It's better that you point out your question in title. You can edit it. $\endgroup$ – Hoseyn Heydari Feb 15 '14 at 12:19
  • $\begingroup$ @Hoseyn:Thanks I have edited it $\endgroup$ – joke Feb 15 '14 at 12:32
0
$\begingroup$

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:');
while (rad<=0)
    disp('Error! Please enter a positive number.');
    rad=input('Enter the radius of cylinder:');
end;

for each of the input variables.

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

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)
%display a menu
disp('Menu');
disp('1. Cylinder');
disp('2. Circle');
disp('3. Rectangle');
%choice selection
i=input('Please select your choice now: ');
if i==1
    disp('well! your choice is cylinder');
r=input('enter radius: ');
%error check
while(r<=0)
    disp('Error! Please enter a positive number')
    r=input('enter radius again: ');
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
    disp('well! your choice is cycle')
r=input('enter radius: ');
%error check
while(r<=0)
    disp('Error! Please enter a positive number')
    r=input('enter radius again: ');
end
%area calculated and print
area=2*pi*r;
fprintf('The area is %.2f\n',area);
elseif i==3
    disp('well! your choice is rectangle')
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
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.