Thread: Having a problem with gcd

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    36

    Having a problem with gcd

    I'm having a problem with gcd. I don't know why it gives me a red squiggly under the gcd(lower case) at this line
    GCD = gcd(y, x % y);

    Any help?


    Code:
    // Greatest Common Divisor.cpp : Defines the entry point for the console application.
    //
    
    //	=========
    //	Libraries
    //	==================
       #include "stdafx.h"
       #include <iostream>
       #include <math.h>
       #include <stdio.h> 
    //	==================
    
    //  ==========
    //  Namespaces
    //	====================
    	using namespace std;
    //  ====================
    
    //  ===================
    //	Function Prototypes
    //	=====================
    	void WelcomeBanner();
    	void GetIntegers(int&, int&);
    //	=====================
    
    
    
    // ==============
    
    	int main( ) { // main()
    
    		int x;
    		int y;
    		int GCD;
    
    		WelcomeBanner();
    		GetIntegers(x, y);
    
    		if (y == 0 ){
    			
    			cout << "GCD = " << x << endl;
    		}
    		else
    			GCD = gcd(y, x % y);
    			cout << "GCD = " << GCD << endl;
    		
    	
    		
    
    
    	return 0;
    	}// main()
    //	========
    
    
    //  =========================
    //	Function WelcomeBanner()
    //	=========================
    	void WelcomeBanner(){
    
    		cout << "================================================" << endl;
    		cout << "Welcome to my program that will give you the   |" << endl << "greatest common divisor of your two numbers =) |" << endl;
    		cout << "================================================"<< endl << endl;
    
    	}// Welcome banner
    //	==================
    
    //	====================
    //	Function GetIntegers
    //	====================
    	void GetIntegers(int& num1, int& num2){
    
    		cout << "Please enter an integer that you want me to find the GCD of." << endl;
    		cin >> num1;
    
    		cout << "Please enter another integer that you want me to find the GCD of." << endl;
    		cin >> num2;
    
    	}//GetIntegers
    //	==============

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Bump!

  3. #3
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    Have you written the gcd function somewhere becuase there is no gcd function in cmath.
    "All that we see or seem
    Is but a dream within a dream." - Poe

  4. #4
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Well in my book it says

    gcd(x, y) = x if y = 0
    gcd(y,x%y) if y =/= 0

    So I'm a little confused..

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your book gave you the mathematics that can be used to implement the function. It's your turn now
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    -bleh-
    Join Date
    Aug 2010
    Location
    somewhere in this universe
    Posts
    463
    That's the concept of GCD. You have to write that gcd function yourself.
    Code:
    int gcd(int x, int y)
    {
      translate that part into code here
    }
    "All that we see or seem
    Is but a dream within a dream." - Poe

  7. #7
    Registered User
    Join Date
    Aug 2009
    Posts
    36
    Awwww. Okokok. I can do that

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  2. Visual Studio Linker problem or my problem?
    By OOPboredom in forum C Programming
    Replies: 2
    Last Post: 04-13-2004, 12:32 AM
  3. syntax linked list problem & struct problem
    By beely in forum C Programming
    Replies: 5
    Last Post: 11-11-2002, 09:14 AM
  4. Texture Problem(I got the NeHe tut working, but I have a problem)
    By SyntaxBubble in forum Game Programming
    Replies: 2
    Last Post: 12-02-2001, 10:40 PM