2009-10-23 11 views
6

正しい分割関数を持つプログラムを作成しようとしました。私のC++分割プログラムはコンパイルされません

#include <iostream> 

using namespace std; 

double x,y,z,a; 

double divide(x,y) { 
    if (x >= y) { 
     x=z; 
     z=y; 
     y=x; 
     return(x/y); 
    } 
    else 
     return(y/x); 
} 

int main() 
{ 
    double x,y,z ; 
    cout << "Enter x " <<endl; 
    cin >> x; 
    cout << "Enter y " <<endl; 
    cin >> y; 
    a = divide (x,y); 
    cout << a <<endl; 

    system("pause"); 
    return 0; 
} 

そして、私は2個のエラーがあります: 私のコードはでした{ライン上

expected `,' or `;' before '{' token 

を。 a = divide (x, y);ライン上で右ダブルデバイド(x, y)ラインの下

そして、別のエラー

divide cannot be used as a function 

。 コード:ブロックを使用しています

答えて

13

関数divideに適切な関数シグネチャを指定する必要があります。具体的には、関数の引数は、その型が欠落しています

double divide(double x, double y) 
{ 
    ... 
} 

ます。また、if文の中で、各ブロックのスコープを作成する必要があります。

if (x > y) 
{ 
    ... 
} 
else 
{ 
    ... 
} 
+1

ブロックは、コードの唯一の行が含まれている場合に技術的に、 'if' /' else'/'他if'ブロックの中括弧は必要とされません。 –

+6

しかし、そこに括弧があることを間違えた場合には、後で何時間も悩まされることはないので、おそらくそれらを置くべきです。 :) –

3

if文で括弧はしないでくださいelseブロックを回ります。そこに別のペアの括弧が必要です。試してみてください:

if (x >= y){ 
     x=z ; 
     z=y ; 
     y=x ; 
     return(x/y); 
    } 
    else { 
     return(y/x); 
    } 

「他」の後に1行のコードの周りにカッコ(の第二セットは厳密には必要ありません。あなたがいる場合や他のオフ中括弧を残すことができ、ブロックが一つだけある場合

また、分割機能では、xy変数の型を指定していません。型を指定する必要があります。あなたが書いた他の変数と同じように、それらのためにあなたは書いたでしょう。

double x,y,z,a ; 

...機能の外ですが、それは役に立たない。それは、、yz、およびaという名前の二重変数を新しい関数と定義しています。

+0

+1は、 'オプション'の中カッコに言及しています。私は、コードの2行目を追加するときに中括弧を追加することを忘れているので、ソースコードには多くのバグがあります。例えば、ある種のデバッグstd :: coutを 'return y/x'の行の前に置くことを考えてください。私はゆっくりとメンテナンスを容易にするために、中断を6ヶ月間続けているということを私のチームに説得しています! –

0

if ... elseの括弧を修正しました。また、関数のパラメータに型を定義する必要があります。

using namespace std; 

     double x,y,z,a ; 

double divide (double x, double y) 
    { 
     if (x >= y){ 
      x=z ; 
      z=y ; 
      y=x ; 
      return(x/y); 
     } 
     else 
     { 
      return(y/x); 
     } 
    } 

    int main() 
{ 
    double x,y,z ; 
    cout << "Enter x " <<endl; 
    cin >> x ; 
    cout << "Enter y " <<endl; 
    cin >> y ; 
    a = divide (x,y); 
    cout << a <<endl; 

     system("pause"); 
    return 0; 
} 
0
#include <iostream> 

using namespace std; 

// divides x/y 
double divide (x,y) 
{ 
    if(y != 0) 
    { 
     /*{} <- this is called a scope. 
     it is important to keep track of scopes. 
     each function has it's own scope 
     each loop or an if instruction can have it's own scope 
     in case it does - all the instructions from the scope will be executed 
     in case it doesn't - only the 1st instruction after the if/else/for/while etc. will be executed 

     Here's another funny thing about scopes : 
     { 
      double x; // this variable exists ONLY within this scope 
     } 
     { 
      // y doesn't exist here 
      { 
       double y; // y exists here. it's local 
      } 
      // y doesn't exist here 
     } 
     */ 
     return x/y; 
    } 
    else 
     return 0; 
} 

int main() 
{ 
    double x,y; 
    cout << "Enter x " <<endl; 
    cin >> x ; 
    cout << "Enter y " <<endl; 
    cin >> y ; 
    double a = divide (x,y); 
    cout << a <<endl; 
    cin; 
    return 0; 
} 
+2

パラメータタイプがありません。 – dirkgently

関連する問題