Problem with Matlab Repeating menu with a loop, with user determining when it stops

6 views (last 30 days)
I have to make a repeating code that allows the user to select one of four options. One calculates the area of either a rectangle or triangle that they enter the specs for, one calculates the perimeter, another exits the program, and the first option allows them to enter the specs for the shape.
I want this menu to repeat infinitely until user decides its time to exit:
Geometric Calculator. Please select an option below:
Press 1 to enter values and shape code (r/R for Rectangle and t/T for Triangle).
Press 2 to calculate the area of said shape.
Press 3 to calculate the perimeter of said shape.
Press 4 to exit program.
selection=input('Enter a selection>> ');
------my main script-----
menu();
global selection;
global sideone;
global sidetwo;
global shapecode;
if selection==1 || selection==2 || selection==3 || selection==4
if selection ==1
sideone=input('Please enter a value for the first side>> ');
sidetwo=input('Please enter a value for the secon side>> ');
shapecode=input('Please enter the shape code (r/R for Rectangle and t/T for triangle)>> ');
menu();
elseif selection==2
calcArea();
menu();
elseif selection==3
calcPerim();
menu();
else
disp('Thank you for using the geometric calculator');
end
else
disp('Please enter a valid menu option (1 through 4)');
end
----menu----
function [] = menu()
disp({'Press 1 to enter values and shape code (r/R for Rectangle and t/T for Triangle).',
'Press 2 to calculate the area of said shape.',
'Press 3 to calculate the perimeter of said shape.',
'Press 4 to exit program.'})
global selection;
selection=input('Enter a selection>> ');
end
----calcArea----
function [] = calcArea()
global sideone;
global sidetwo;
global shapecode;
area=0;
if shapecode=='R' || shapecode=='r'
area=sideone*sidetwo;
fprintf('The area is %0.3f \n',area);
elseif shapecode=='T' || shapecode=='t'
area=.5*sideone*sidetwo;
fprintf('the area is %0.3f \n',area);
end
----calcPerim----
function [] = calcPerim()
global sideone;
global sidetwo;
global shapecode;
perim=0;
if shapecode=='R' || shapecode=='r'
perim=sideone+sideone+sidetwo+sidetwo;
fprintf('The perimeter is %0.3f \n',perim);
elseif shapecode=='T' || shapecode=='t'
perim=sideone+sidetwo+sqrt((sideone^2)+(sidetwo^2));
fprintf('the perimeter is %0.3f \n',perim);
end
The error i get:
>> quiz7wk10
'Press 1 to enter values and shape code (r/R for Rectangle and t/T for Triangle).'
'Press 2 to calculate the area of said shape.'
'Press 3 to calculate the perimeter of said shape.'
'Press 4 to exit program.'
Enter a selection>> 2
Operands to the || and && operators must be convertible to logical scalar values.
Error in quiz7wk10 (line 7)
if selection==1 || selection==2 || selection==3 || selection==4

Answers (0)

Categories

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