开发者

Why does my C++ divide program not compile

开发者 https://www.devze.com 2022-12-09 02:08 出处:网络
I tried to make a program that has a correct Divide function. My code was: #include <iostream> using namespace std;

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 <&开发者_C百科lt;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


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
{
    ...
}


The braces in an if statement don't go around the else block. You need a separate pair of braces there. Try:

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

The second set of braces (around the one line of the code after the 'else' aren't strictly necessary; you can leave the braces off an if or an else if the block is only one line long. But while you're new you probably shouldn't, as it's easy to make mistakes that way.

Also, you have not provided types for the x and y variables in your divide function. You must specify types for them, just as you would for any other variable. You've written

    double x,y,z,a ;

...outside of the function, but that doesn't help; it defines new double variables named x, y, z,and a, completely independent of the ones in your function.


Corrected your braces in your if...else. also need to define a type in your function's parameters.

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


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

精彩评论

暂无评论...
验证码 换一张
取 消