- #1
gfd43tg
Gold Member
- 950
- 50
Homework Statement
Using the switch-case construction, write code that take a variable named Shape containing a
string and assigns to the variable numSides the number of sides of the shape named in the variable
Shape. Your code should be able to return the number of sides for a triangle, square, pentagon,
hexagon, heptagon, or octagon. If the variable Shape contains a string that is not listed above,
assign NaN to the variable numSides and display one of the following two warnings:
If the string stored in Shape ends in `gon', you should display the following warning: "I
don't know how many sides a <string stored in Shape> has.".
If the string stored in Shape does not end in `gon', your code should display the following
warning: "I don't know what a <string stored in Shape> is.".
Homework Equations
The Attempt at a Solution
I am stuck about how to enter in the error messages, here is what I have working so far.
Code:
switch Shape
case 'triangle'
numSides = 3;
case 'square'
numSides = 4;
case 'pentagon'
numSides = 5;
case 'hexagon'
numSides = 6;
case 'heptagon'
numSides = 7;
case 'octagon'
numSides = 8;
otherwise
numSides = NaN;
end
This is how I have been trying to implement an error message
Code:
if numSides = NaN
error = 1;
if error
disp( 'I don''t know how many sides a <string stored in Shape> has.')
Thank you.