Thread: Help with my euclidean algorithm program?

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    3

    Help with my euclidean algorithm program?

    The error I get is Line 8: error: 'euclidean_algorithm' was not declared in this scope

    Does anyone know what's wrong?


    Code:
    int main(int argc, char *argv[]) {
    	int x,y,aa;
    
    	cout << "enter number 1: " << endl;
    	cin >> x;
    	cout << "enter number 2: " << endl;
    	cin >> y;
    	aa=euclidean_algorithm(x,y);
    }
    
    
    int euclidean_algorithm(int a, int b) {
    
    	// the first thing to do is to make sure a > b. If it is not, swap them.
    
    	if (b > a) { int t = a; a = b; b = t; }
    
    	// now we run the algorithm in a loop
    	while (b != 0) {
    	    int temp = a % b;
    	    a = b;
    	    b = temp;
    	}
    
    	cout << "the greatest common denominator is " << a << endl;
    }

  2. #2
    Registered User
    Join Date
    Dec 2007
    Posts
    2,675
    The C++ compiler reads files from the top down. When you put euclidean_algorithm as a function call in your main, the compiler goes looking through what it's already found to make sure there's a function with that name. But alas, there is not in this case. You need to prototype the function before you call it in your code.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    3
    Thanks, I did a line to do that, and now the error is at the end bracket on the last line -

    line 28: warning: control reaches end of non-void function


    what does this mean?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    euclidean_algorithm is declared as returning int (returning anything other than void means the function is "non-void", according to your compiler).

    There is no return statement before the closing brace at the end of that function. One is needed of the form "return some_integral_value;"
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. BOOKKEEPING PROGRAM, need help!
    By yabud in forum C Programming
    Replies: 3
    Last Post: 11-16-2006, 11:17 PM
  2. I need some help with my program please.
    By agentxx04 in forum C Programming
    Replies: 9
    Last Post: 09-26-2004, 07:51 AM
  3. Results for the Encryption Contest -- June 23, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 07-07-2002, 08:04 AM
  4. Contest Results - May 27, 2002
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 18
    Last Post: 06-18-2002, 01:27 PM
  5. My program, anyhelp
    By @licomb in forum C Programming
    Replies: 14
    Last Post: 08-14-2001, 10:04 PM