2011-01-24 66 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 2011-01-24 19:58:37

回答

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 2011-01-24 19:59:19

2

您從未設置d的值,但您仍然在交換機中使用它。

0

您從不指定變量d