Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does my C++ divide program not compile

Tags:

c++

I tried to make a program that has a correct Divide function. My code was:

#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;
}

And I have 2 errors:

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

on the { line. Right under the double divide (x, y) line

And another error

divide cannot be used as a function

on the a = divide (x, y); line. I am using Code: Blocks

like image 349
rafael Avatar asked Oct 23 '09 19:10

rafael


1 Answers

You need to specify a proper function signature for the function divide. Specifically, the arguments to the function are missing their types:

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

You also need to create a scope for each block in an if statement:

if (x > y)
{
    ...
}
else
{
    ...
}
like image 162
Steve Guidi Avatar answered Nov 15 '22 00:11

Steve Guidi