2011-01-24 3 views
0

자동으로 기본값을 수행하며 내 경우는 수행하지 않습니다. 내가 뭘 잘못하고 있는거야?스위치 케이스에 문제가 있습니까?

#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"); 
    } 
} 
+4

D는 초기화되지 내가 무슨 값을 할당해야 – Mauricio

답변

3

그것은 당신의 scanf와 같다() 호출 :

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

가 있어야한다 :

scanf ("%c",&d); 

왜 당신이 이제까지 세 개의 부동 소수점 입력을 받아들이는 것은 프롬프트의 텍스트 주어진 의미가 생각!?

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

d에는 값을 할당하지 않으므로 초기화되지 않습니다.

+0

:

그러나 그래서 당신이 실제로 무엇을해야하고, 그 이후의 입력 전화로 당신에게 문제를 일으킬거야? – EGP

+0

@Hany : 재미있는 질문입니다. 어떤 가치를 갖고 싶습니까? 아마도''p '',''a'',''h''의 값을 가지기를 원합니까? –

+1

scanf에서 d 값을 읽지 않습니다. 스위치 전에 읽어야하는 옵션 (P, A 등)을 입력 할 수 있습니다. – Mauricio

2

d의 값은 설정하지 않지만 스위치에서는 계속 사용합니다.

0

변수 d을 할당하지 마십시오.

관련 문제