Switch case with strings?

336 views (last 30 days)
Shannon
Shannon on 31 Oct 2013
Commented: Walter Roberson on 8 Sep 2020
I'm trying to create a loop which takes the name of a regular polygon from the user. It should be case insensitive and the program should have cases for a pentagon, hexagon, heptagon, octagon, and nonagon all with fixed side length 10 (I have precalculated the areas, I just want the program to output the numbers I give it). If a typo is made or the user inputs an unrecognized shape name, the program should output an error message and run the loop again. The user is supposed to have 3 chances to type a shape name correctly before the program terminates, and if they always type one correctly, the loop should run infinitely.
This is all I have so far, and it's not working:
if true
t=input('Enter shape name: ');
ShapeName=char(t);
switch ShapeName
case char('pentagon')
fprintf('The area of the pentagon is 172.05 m^2.')
case char('hexagon')
fprintf('The area of the hexagon is 259.81 m^2.')
case char('heptagon')
fprintf('The area of the heptagon is 363.39 m^2.')
case char('octagon')
fprintf('The area of the octagon is 482.84 m^2.')
case char('nonagon')
fprintf('The area of the nonagon is 618.18 m^2.')
otherwise
disp('Error, no such shape is found! Try again!')
end
end
Please help! I'm totally lost.

Answers (1)

sixwwwwww
sixwwwwww on 31 Oct 2013
Dear Shannon, maybe you can do as follows:
count = 0;
while count < 3
ShapeName = input('Enter shape name: ', 's');
switch ShapeName
case 'pentagon'
fprintf('The area of the pentagon is 172.05 m^2.\n')
count = 0;
case 'hexagon'
fprintf('The area of the hexagon is 259.81 m^2.\n')
count = 0;
case 'heptagon'
fprintf('The area of the heptagon is 363.39 m^2.\n')
count = 0;
case 'octagon'
fprintf('The area of the octagon is 482.84 m^2.\n')
count = 0;
case 'nonagon'
fprintf('The area of the nonagon is 618.18 m^2.\n')
count = 0;
otherwise
fprintf('Error, no such shape is found! Try again!\n')
count = count + 1;
end
end
I hope it helps. Good luck!
  5 Comments
Steven Lord
Steven Lord on 8 Sep 2020
If you want the switch / case not to take case into account, switch on lower(ShapeName).
Walter Roberson
Walter Roberson on 8 Sep 2020
Mitch, the cases that you use in switch need to be a compatible datatype with the switch expression. You are switching on a character vector, but your cases are all logical. You would need
count = 0;
ErrorMsg = {'Error, no such shape is found! Try again!', 'Error, no such shape is found! Program terminated.'};
while count < 2
ShapeName = input('Enter shape name: ', 's');
switch true
case (strcmpi(ShapeName,'pentagon')==1)
fprintf('The area of the pentagon is 172.05 m^2.\n')
case (strcmpi(ShapeName,'hexagon')==1)
fprintf('The area of the hexagon is 259.81 m^2.\n')
case (strcmpi(ShapeName,'heptatagon')==1)
fprintf('The area of the heptagon is 363.39 m^2.\n')
case (strcmpi(ShapeName,'octatagon')==1)
fprintf('The area of the octagon is 482.84 m^2.\n')
case (strcmpi(ShapeName,'nontagon')==1)
fprintf('The area of the nonagon is 618.18 m^2.\n')
otherwise
fprintf('%s\n', ErrorMsg{count + 1});
count = count + 1;
end
end
which is ugly but legal.
Steven's recommendation to switch on lower(ShapeName) is much better coding.

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!