tags:

views:

290

answers:

4

Hi all , i'm totally new to c++ ... i tried to make a program that have 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 got 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... and using Code::Blocks

help me plz i'm totally new to this :D note : if there're any programming errors beside those two plz tell me :)

thx

+10  A: 

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
{
    ...
}
Steve Guidi
Technically, the braces of an `if`/`else`/`else if` block are not required if the block contains only one line of code.
Steve Guidi
But a lot of times you should probably still put them since it'll save you hours of trouble later on if you make the mistake of thinking there *were* braces there. :)
Andrew Song
+2  A: 

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.

David Seiler
+1 for mentioning the 'optional' braces. I have witnessed many bugs in source code because people forget to add braces when they add the second line of code. For example - consider putting some kind of debug std::cout before the 'return y/x' line. I'm slowly convincing my team that always putting braces in makes for easy maintenance 6 months down the line!
Steve Folly
A: 

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;
}
AcousticBoom
A: 
#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;
}
Maciek
You are missing parameter types.
dirkgently