Hi
Have written this code for a program that requires the user to either enter 1 or 2 dependant on whether the program solves the problem of finding the divisors of any given to numbers by recursive or iteration methods.

#include<iostream>

using namespace std; 

int recursiveGCD(int ,int );   
int iterativeGCD(int ,int );
int input;

void main()

{
   int x,y;
   cout<<"Please enter the two numbers : ";
   cin>>x>>y;
   cout<<"Enter 1 for recursive calculation, 2 for iterative";
   cin>>input;
   { 
	if input == 1
	return recursiveGCD;
	if input == 2
	return iterativeGCD;
   }
   cout<<"The Greatest Common Divisor of ("<<x<<","<<y<<") = " << recursiveGCD(x,y)<<endl;
}


int recursiveGCD(int x,int y)
{
   if(y>x)return recursiveGCD(y,x);
   if(x==y)return x;
   if(x%y==0)return y;
   return recursiveGCD(x,x-y);

}

int iterativeGCD(int x,int y)
{	
	if (y == 0)
		return x;
	else 
		return iterativeGCD(y, x % y);
}

However I am getting the error C2061, saying that input is undeclared when I seem to have to declared it at the top??

Many Thanks

int recursiveGCD(int ,int );   
int iterativeGCD(int ,int );
int input;

The first two are fine (function prototypes).

However the last line
int input;
when I was first reading I was thinking perhaps it was meant to be a function. If that was the original intention, then you would have needed the necessary parenthesis to have it appear as a function

int input();

HOWEVER, the way you're using it you want to go with what Lokolo said in the above post, declare input in your main function

int main
{
    int input;
...
}

hi guys thanks for the quick replies however I did try this and it still throws up the error even when it is changed?

#include<iostream>

using namespace std; 

int recursiveGCD(int ,int );   
int iterativeGCD(int ,int );

int main()
{
   int input;
   int x,y;
   cout<<"Please enter the two numbers : ";
   cin>>x>>y;
   cout<<"Enter 1 for recursive calculation, 2 for iterative";
   cin>>input;
  
   { 
	if input == 1
	return recursiveGCD;
	if input == 2
	return iterativeGCD;
   }
   
   cout<<"The Greatest Common Divisor of ("<<x<<","<<y<<") = " << recursiveGCD(x,y)<<endl;
}


int recursiveGCD(int x,int y)
{
   if(y>x)return recursiveGCD(y,x);
   if(x==y)return x;
   if(x%y==0)return y;
   return recursiveGCD(x,x-y);

}

int iterativeGCD(int x,int y)
{	
	if (y == 0)
		return x;
	else 
		return iterativeGCD(y, x % y);
}

1. Next time try to tell us which line it is.

2. If( ) has brackets.

i.e.

if input == 1
return recursiveGCD;
if input == 2
return iterativeGCD;

Should be

if (input == 1)
return recursiveGCD;
if (input == 2)
return iterativeGCD;

That's not where the problem lies.

{ 
	if input == 1
	return recursiveGCD;
	if input == 2
	return iterativeGCD;
   }

is wrong both from the syntax point of view and logically. You miss some parentheses and you try to call iterativeGCD and recursiveGCD (that are functions) without passing them their arguments (x and y in this case). You should call a function like this recursiveGCD(x, y) . Then I think you don't want to return their output: in fact you try to print it with cout just in the following line. You should store their returned value in a variable and then print that value or directly put the cout instruction inside the if.

Just another thing: both your function are recursive, even the one named iterativeGCD.

thanks for your help, think i'll go back to the drawing board really! cheers

A common main function appears as

int main()
{
    // code
    return 0;
}

Yours appears like this:

int main()
{
// little bit of code
    { // unecessary brackets
        if(some contidion)
            return Function1; // trying to return a value
        else
            return Function2; // trying to return a value
    } // more unessary brackets
// more code
}  // oops no return 0

You need to take out the brackets and give the functions something to store the values in (and pass their parameters.

int main()
{
    int getRecurs, getItera;
// ...
    if (input==1)
        getRecurs = recursiveGCD(PUT PARAMETERS!!!);
    
    if (input == 2)
        getItera = iterativeGCD(PARAMETERS);
// ....
return 0;
}

I still love you, even if no one else does :)

>// unecessary brackets
Which can be used to introduce a new scope without adding a one-off function or doing something dumb like:

if ( true ) {
  // Stuff
}

You'd see it more in C prior to C99 where you want to restrict the scope of a for loop control variable:

/* i isn't visible here */

{
  int i;

  for ( i = 0; i < N; i++ ) {
    /* Do stuff */
  }
}

/* nor is i visible here. yay */

>// oops no return 0
That's perfectly legal in C++. If you omit the return value from main, 0 will be returned automagically.

you may be on crack choco so the validity of the I love you statement may be slightly in question, but the feeling is mutual :). Got it working now though thanks to everyone for replies, you helped a retard and that must make you happy.

This article has been dead for over six months. Start a new discussion instead.