Need help with switch

• 02-25-2003
3kgt
Need help with switch
Can anyone tell me whats wrong with this switch statement?
[code]
* Vanessa Dean's Assignment 2
The Shape Information Page */
#include <stdio.h>
#include <math.h>
#define RIGHT_TRIANGLE 1
#define EQUILATERAL_TRIANGLE 2
#define RECTANGLE 3
#define CUBE 4
#define SPHERE 5
#define PI 3.14
#define AREA 1
#define PERIMETER 2
#define VOLUME 1
#define SURFACE_AREA 2

int main()
{
float area = 0, perimeter = 0, height = 0, base = 0,
hypoteneuse = 0, volume = 0, radius = 0,
surfaceArea = 0, side = 0, result = 0,
shape = 0;

printf( "Welcome to the Shape Information "
"page!\n\n");

printf( "Type the number in the parenthesis "
"to calculate information\nfor one "
"of the following shapes:\n\n" );

printf( "(1) Right Triangle (2)Equilateral "
"Triangle (3) Rectangle\n"
"(4) Cube (5) Sphere\n\n" );
scanf( "%d", &shape );

switch ( shape ) {

case RIGHT_TRIANGLE:
printf( "Input base and height:" );
scanf( "%f%f", &base, &height );

printf( "Select (1) area or (2) perimeter:" );
scanf( "%d", &result );

if ( result = AREA )
area = 0.5 * base * height;
printf( "Area: %f\n", area );
else
hypoteneuse = sqrt( pow( base, 2 ) +
pow( height, 2 ) );
perimeter = base + height + hypoteneuse;
printf( "Perimeter: %f\n", perimeter );
break;

case EQUILATERAL_TRIANGLE:
printf( "Input length of side:" );
scanf( "%f", &side );

printf( "Select (1) area or (2) perimeter:" );
scanf( "%d", &result );

if( result = AREA )
area = 0.25 * pow( side, 2 ) * sqrt( 3 );
printf( "Area: %f\n", area );
else
perimeter = 3 * side;
printf( "Perimeter: %f\n", perimeter );
break;

case RECTANGLE:
printf( "Input base and height:" );
scanf( "%f%f", &base, &height );

printf( "Select (1) area or (2) perimeter:" );
scanf( "%f", &result );

if ( result = AREA )
area = base * height;
printf( "Area: %f\n", area );
else
perimeter = base + height;
printf( "Perimeter: %f\n", perimeter );
break;

case CUBE:
printf( "Input length of side:" );
scanf( "%f", &side );

printf( "Select (1) volume (2) surface area:" );
scanf( "%d", &result );

if ( result = VOLUME )
volume = pow( side, 3 );
printf( "Volume: %f\n", volume );
else
surfaceArea = 6 * pow( side, 2 );
printf( "Surface area: %f\n", surfaceArea );

case SPHERE:
printf( "Input radius:" );
scanf( "%f", &radius );

printf( "Select (1) volume (2) surface area:" );
scanf( "%d", &result );

if ( result = VOLUME )
volume = ( (4/3) * PI * radius );
printf( "Volume: %f\n", volume );
else
surfaceArea = 4 * PI * pow( radius, 2 );
printf( "Surface area: %f\n", surfaceArea );

}

printf( "The number %d is an invalid choice: "
"program will terminate.\n ", shape );

return 0;

}

[\code]
• 02-25-2003
quzah
Don't use floating point variables for switches. Switches require integeral type variables.

On a side note, use this slash for code tags: /

Quzah.
• 02-26-2003
Prelude
Don't forget that a conditional with more than one statement must be surrounded by a block:
Code:

```if ( something )   statement 1;   statement 2; else /* Oops! Floating else statement illegal */   statement 3;```
should be
Code:

```if ( something ) {   statement 1;   statement 2; } else /* Good */   statement 3;```
-Prelude