2016-03-21 12 views
-2

私はコードを修正し、整数を2で割るオプションを追加しなければならない宿題があります。しかし、私のセクションを追加するとエラーメッセージが表示され続けます。新しい関数を作成するときにエラーメッセージが表示される

#include <stdio.h> 

int main() 

{ 

/* variable definition: */ 

int intValue, menuSelect,Results; 
float floatValue; 

intValue = 1; 

// While a positive number 

while (intValue > 0) 

{ 

printf ("Enter a positive Integer\n: "); 

scanf("%d", &intValue); 

if (intValue > 0) 

{ 

printf ("Enter 1 to calculate Square, 2 to Calculate Cube, 3 to divide by 2 \n: "); 

scanf("%d", &menuSelect); 

if (menuSelect == 1) 

{ 

    // Call the Square Function 

    Results = Square(intValue); 

    printf("Square of %d is %d\n",intValue,Results); 

} 

else if (menuSelect == 2) 

{ 

    // Call the Cube function 

    Results = Cube(intValue); 

    printf("Cube of %d is %d\n",intValue,Results); 

} 

else if (menuSelect == 3) 

{ 

    //Call the half function 

    Results = divide2(floatValue); 

    printf("Half of %d is %f\n", intValue,Results); 

} 

else 

    printf("Invalid menu item, only 1 or 2 is accepted\n"); 

}  

}  

return 0; 

} 

/* function returning the Square of a number */ 

int Square(int value) 

{ 

return value*value; 

} 

/* function returning the Cube of a number */ 

int Cube(int value) 

{ 
return value*value*value; 

} 

//Function returning the half of a number 

float divide2(float value) 

{ 
return value/2; 

} 

そして、私は取得していたエラーは以下のとおりです:ここ

はコードである

prog.c: In function 'main': 
prog.c:38:18: warning: implicit declaration of function 'Square'  [-Wimplicit-function-declaration] 
    Results = Square(intValue); 
      ^
prog.c:50:18: warning: implicit declaration of function 'Cube' [-Wimplicit-function-declaration] 
    Results = Cube(intValue); 
      ^
prog.c:62:17: warning: implicit declaration of function 'divide2' [-Wimplicit-function-declaration] 
    Results = divide2(floatValue); 
      ^
prog.c:64:14: warning: format '%f' expects argument of type 'double', but argument 3 has type 'int' [-Wformat=] 
    printf("Half of %d is %f\n", intValue,Results); 
     ^
prog.c: At top level: 
prog.c:101:7: error: conflicting types for 'divide2' 
float divide2(float value) 
^
prog.c:62:17: note: previous implicit declaration of 'divide2' was here 
    Results = divide2(floatValue); 
      ^

コードが間違っていますか?元のコードを実行しているとき、私はメッセージを取得していない

+1

Cコードを書くときには、一度に1つずつコードを理解するようにコンパイラに指示しているとします。コードの他の部分が使用される前に使用する関数を導入する必要があります。あなたの特定の例では、 'main'は' Square'の前にあるので、 'Square'呼び出しを最初に見た時にコンパイラは本当に何をすべきか分かりません。この原則に従って機能を注文し、問題が解決するかどうかを確認してください。 –

+0

'main'関数の前にすべての関数のプロトタイプを使い、その後でそれらを定義するだけです。 – RastaJedi

+1

prototypesを追加するか、関数を実際に呼び出す前に定義します。つまり、main()から呼び出す場合はmain()の前に定義します。 –

答えて

2

あなたは、あなたが呼ばれているポイントの後にそれを定義している場合、関数のプロトタイプを追加する必要があり、それ

例:

ABC(); 

void ABC(){ 
//do some stuff 
} 

ABC()を呼び出す前にvoid ABC();を追加しない限り動作しません。

これも正常に動作します:ここで

void ABC(){ 
//do something 
} 

ABC(); 
+0

私はメインの前に関数を導入することで、コードを動かすことができました。助けてくれてありがとう! –

+3

'ABC();'は不正です、プロトタイプはint ABC(void);またはvoid ABC(void); –

+0

です。 –

0

は(あなたはint型とfloat型の両方にresultsを使用することはできませんので、私はいくつかの変数名を変更し、私もかどうかをチェックするためにループを変更し動作するコードです0が正であるとみなされ、正方形/立方体/半分が0に対して明確に定義されているので、またはが0に等しい)。私は一番上に定義された一つの関数を作ります。小規模なので、1行にするのが簡単で、一般的には1行の場合は上に配置しますが、残りの部分は上にプロトタイプを作成し、後で定義します(両方のスタイルを見ることができます)。

#include <stdio.h> 

int square(int value); 
int cube(int value); 
/* Function returning the half of a number */ 
float divide2(float value) { return value/2; } 

int main() 
{ 
    int number, menu_select, int_result; 
    float float_result; 

    number = 1; 

    // While a positive number 
    while (number >= 0) 
    { 
      printf("Enter a positive Integer\n: "); 
      scanf("%d", &number); 

      if (number > 0) 
      { 
        printf("Enter 1 to calculate Square, 2 to Calculate Cube, 3 to divide by 2\n: "); 
        scanf("%d", &menu_select); 

        switch (menu_select) 
        { 
        case 1: 
          // Call the square Function 
          int_result = square(number); 
          printf("Square of %d is %d\n", number, int_result); 
          break; 
        case 2: 
          // Call the cube function 
          int_result = cube(number); 
          printf("cube of %d is %d\n", number, int_result); 
          break; 
        case 3: 
          // Call the half function 
          float_result = divide2(number); 
          printf("Half of %d is %f\n", number, float_result); 
          break; 
        default: 
          printf("Invalid menu item, only 1 or 2 is accepted\n"); 
        } 
      } 
    } 

    return 0; 
} 

/* Function returning the square of a number */ 
int square(int value) 
{ 
    return value * value; 
} 

/* Function returning the cube of a number */ 
int cube(int value) 
{ 
    return value * value * value; 
} 

私はまた、コードすっきりを作るために、単一のswitchif/else年代のあなたのシリーズを変更しますので、switchの力を見ることができます。私は個人的に使用したことはありませんが、これらの機能はinlineです。

フロート値にprintfのフィールドを指定すると、0.50のようになります。 0.500000の代わりに。ここでは、小数点以下2桁を指定する方法を次のとおりです。あなたが精度を指定したい場合

printf("Here is a float value: %.2f\n", float_var); 

ちょうどあなたが好きに%.2fで「2」に変更します。

1

あなたは、ちょうど下のプロトタイプを使用する必要があります。

int Square(int value); 
int Cube(int value); 
float Divide2(float value); 

あなたが使用プロトタイプをしたくない場合。オプションはすべてのメソッドをメイン関数の上に置いてありますが、これはお勧めできませんが動作します。

プロトタイプは、コンパイラへの参照であり、事前に関数をロードしておく必要があります。

関連する問題