Conditional Operators

1. Jun 19, 2014

Maylis

1. The problem statement, all variables and given/known data
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.".

2. Relevant equations

3. 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 (Text):
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 (Text):
if numSides = NaN
error = 1;
if error
disp( 'I don''t know how many sides a <string stored in Shape> has.')

Of course this is wrong. There are two problems here for me. First, I don't know how to make an error message for two separate errors. Secondly, I don't know how to insert the string into the error message.

Thank you.

2. Jun 19, 2014

Staff: Mentor

You're close

try this pseudo code construct:
Code (Text):

// DO we have an error?
if numSides == NaN:

// WHICH error is it?
if Shape.endswith("gon"):
emsg="We don't know how many sides the "+Shape+" has.";

else:
emsg="We know know what "+Shape+" is.";

// PRINT the error msg
disp(emsg);

3. Jun 19, 2014

.Scott

I'm not sure which language you're using, but I will assume the syntax is OK. But be suspect of the syntax that I suggest. Javascript? Visual Basic Script maybe?

You don't need "error = 1".

The additional code should be placed immediately after the "numSides = Nan;" statement.
First you need to isolate the last three characters from "Shape". Perhaps Shape.Mid(Shape.Length()-3)? The compare that to "gon". You can use an if/then/else construct so...
Code (Text):

...
numSides = Nan;
if Shape.Mid(Shape.Length()-3) = "gon"
disp( 'I don''t know how many sides a ' & Shape & ' has.')
else
disp( 'I don''t know what a ' & Shape & ' is.')
end

4. Jun 19, 2014

Maylis

I am using MATLAB

5. Jun 19, 2014

.Scott

Ahh. so you have regular expressions available to you.
You should search for "\w*gon".
length( regexp(Shape,"\w*gon") ) > 0

6. Jun 19, 2014

Maylis

Jedish, I don't know if the code you wrote is for MATLAB, but I get an operator error when I put it in the command window. I will put in thread titles that I am using MATLAB from now on to avoid confusion.

7. Jun 19, 2014

Staff: Mentor

My code was python-based pseudo code, I figured you would know enough to convert to your language especially since you didn't mention what it was.

8. Jun 19, 2014

Maylis

Not yet, only been programming for about a week!