EGP
EGP

Reputation: 397

What's wrong with my switch-case?

It automatically does the default and not any of my cases. Any idea what I'm doing wrong?

#include <stdio.h>
#include <math.h>

int main ()
{

    float x,y,z,p,a,h;
    int d;

    printf ("\n This program calculates Perimeter, Area, or Hypotenuse of a right triangle based on user choice\n\n\n\n");

    /* Prompt user to select which calculation is to be performed */

    printf ("If you would like to calculate Perimeter, type P\nIf you would like to calculate Area, type A\nIf you would like to calculate 
    Hypotenuse, type H\n\n") ;

    scanf ("%f,%f,%f",&p,&a,&h);


    switch(d)
    {
        case('p'):
            printf("/n You have chosen to do a perimeter calculation/n");
            printf("/n Input the two side lengths of the right triangle separated by a space/n");

            scanf("%f,%f",&x,&y);
            z = pow (x,2) + pow (y,2);
            p = x + y + z;

            printf("\nLength of side A entered=%f\n",x);
            printf("\nLength of side B entered=%f\n",y);
            printf("\nCalculated Perimeter=%f\n",p);

            break;


        case('a'):
            printf("/n You have chosen to do an area calculation/n");
            printf("/n Input the two side lengths of the right triangle separated by a space/n");

            scanf("%f,%f",&x,&y);
            z = pow(x,2) + pow(y,2);
            p = x + y + z;
            a = (x * y) / 2;

            printf("\nLength of side A entered=%f\n",x);
            printf("\nLength of side B entered=%f\n",y);
            printf("\nCalculated area=%f\n",a);

            break;


        case('h'):

            printf("/n You have chosen to do a hypotenuse calculation/n");
            printf("/n Input the two side lengths of the right triangle separated by a space/n");

            scanf("%f,%f",&x,&y);
            z = pow (x,2) + pow (y,2);

            printf("\nLength of side A entered=%f\n",x);
            printf("\nLength of side B entered=%f\n",y);
            printf("\nCalculated Hypotenuse=%f\n",z);

            break;

            default:

                printf("/n wow...how did that even happen. Please put in a valid letter next time. /n");
    }
}

Upvotes: 0

Views: 269

Answers (4)

Clifford
Clifford

Reputation: 93456

It looks like your scanf() call:

scanf ("%f,%f,%f",&p,&a,&h);

should be:

 scanf ("%c",&d);

Why would you ever think accepting three float inputs makes sense given the text of the prompt!?

However doing that will cause you problems with the subsequent input calls, so what you should actually do is:

scanf ("%c",&d);
while( d != '\n' && getchar() != '\n' ) 
{
    // do nothing but flush to the end of the input line
}

Upvotes: 3

John Bode
John Bode

Reputation: 123458

You never assign the variable d.

Upvotes: 0

kynnysmatto
kynnysmatto

Reputation: 3892

You never set the value of d but you still use it in the switch.

Upvotes: 2

James McNellis
James McNellis

Reputation: 354979

You never assign any value to d so it is uninitialized.

Upvotes: 8

Related Questions