2009-10-23 100 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 
{线

。右键下方的双分频(x, y)线

而另一个错误

divide cannot be used as a function 

a = divide (x, y);线。 我正在使用代码:块

回答

13

您需要为函数divide指定一个适当的函数签名。具体来说,函数的自变量是缺少它们的类型:

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

您还需要在if语句来创建每个块的范围:在

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

从技术上讲,如果块仅包含一个行代码不需要一个'if' /'else' /'别的if'块的大括号。 – 2009-10-23 19:59:47

+6

但是很多时候你应该把它们放进去,因为如果你犯了错误,那么它会在以后节省你几个小时的麻烦。 :) – 2009-10-23 21:03:17

3

大括号if语句不绕过else块。你需要一对独立的大括号。尝试:

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

后的“其他”第二组括号(周围的代码的一条线是不是绝对必要的,你可以离开括号关闭的,如果或者否则,如果该块只有一个线长。但是,当你新的你可能不应该,因为它很容易犯错误的方式。

而且,你没有提供类型在分功能的xy变量,你必须指定类型对于他们来说,就像你对任何其他变量一样。你写的

double x,y,z,a ; 

...功能之外,但没有帮助;它定义了新的双变量名为x,y,za,完全独立于您的函数中的变量。

+0

+1提''可选'括号。我目睹了源代码中的许多错误,因为人们在添加第二行代码时忘记添加大括号。例如 - 考虑在'return y/x'行之前加入某种调试std :: cout。我正在慢慢说服我的团队,总是把支架放在一个容易维护的地方,让他们在6个月的时间内保养! – 2009-10-23 20:58:47

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 2009-10-23 20:10:39

相关问题