Need help with switch

This is a discussion on Need help with switch within the C Programming forums, part of the General Programming Boards category; Can anyone tell me whats wrong with this switch statement? [code] * Vanessa Dean's Assignment 2 The Shape Information Page ...

  1. #1
    Registered User
    Join Date
    Feb 2003
    Posts
    29

    Arrow 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]

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Don't use floating point variables for switches. Switches require integeral type variables.

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

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    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
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  2. ascii rpg help
    By aaron11193 in forum C Programming
    Replies: 18
    Last Post: 10-29-2006, 01:45 AM
  3. Switch
    By cogeek in forum C Programming
    Replies: 4
    Last Post: 12-23-2004, 06:40 PM
  4. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 04:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21