| | |
Recursive program issue
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2008
Posts: 15
Reputation:
Solved Threads: 0
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.
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
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.
C++ Syntax (Toggle Plain Text)
#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
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
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
C++ Syntax (Toggle Plain Text)
int main { int input; ... }
I would love to change the world, but they won't give me the source code
•
•
Join Date: Nov 2008
Posts: 15
Reputation:
Solved Threads: 0
hi guys thanks for the quick replies however I did try this and it still throws up the error even when it is changed?
C++ Syntax (Toggle Plain Text)
#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); }
Last edited by nedsnurb; Nov 19th, 2008 at 12:30 pm.
•
•
Join Date: Jun 2008
Posts: 182
Reputation:
Solved Threads: 18
That's not where the problem lies. 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
Just another thing: both your function are recursive, even the one named iterativeGCD.
C++ Syntax (Toggle Plain Text)
{ if input == 1 return recursiveGCD; if input == 2 return iterativeGCD; }
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.
A common main function appears as
Yours appears like this:
You need to take out the brackets and give the functions something to store the values in (and pass their parameters.
I still love you, even if no one else does
C++ Syntax (Toggle Plain Text)
int main() { // code return 0; }
Yours appears like this:
C++ Syntax (Toggle Plain Text)
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.
C++ Syntax (Toggle Plain Text)
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
Last edited by chococrack; Nov 19th, 2008 at 12:52 pm.
I would love to change the world, but they won't give me the source code
>// unecessary brackets
Which can be used to introduce a new scope without adding a one-off function or doing something dumb like:
You'd see it more in C prior to C99 where you want to restrict the scope of a for loop control variable:
>// oops no return 0
That's perfectly legal in C++. If you omit the return value from main, 0 will be returned automagically.
Which can be used to introduce a new scope without adding a one-off function or doing something dumb like:
C++ Syntax (Toggle Plain Text)
if ( true ) { // Stuff }
c Syntax (Toggle Plain Text)
/* i isn't visible here */ { int i; for ( i = 0; i < N; i++ ) { /* Do stuff */ } } /* nor is i visible here. yay */
That's perfectly legal in C++. If you omit the return value from main, 0 will be returned automagically.
I'm here to prove you wrong.
![]() |
Similar Threads
- C++ Fibonacci program help (C++)
- Recursive Palindrome (Java)
- good recursive program (C)
- GAAAH! Memory issue? (C++)
Other Threads in the C++ Forum
- Previous Thread: How to read CVS Tag of a fille/directory from CPP Program
- Next Thread: Inheritence, .h files and its a long one
| Thread Tools | Search this Thread |
api array based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock wordfrequency wxwidgets






