2011-01-24 14 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); 

は、なぜあなたはこれまでに3つのフロート入力を受け付けることは、プロンプトのテキストを指定して理にかなっていると思うだろう!

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を割り当てないでください。

関連する問題