I am completly lost on this could someone point me in the right direction into where I am going wrong with this code.

#include <iostream> 

using namespace std ;

int main() 
{ 

    int x ;
	int y ;
	int temp ;
	int remainder ;

    // read in the two integers

    cout << endl ;
    cout << "Enter the first number (positive integer) : " ; 
    cin >> x ;
    cout << "Enter the second number (positive integer) : " ; 
    cin >> y ;

    //echo inputs

    cout << "Input numbers are: " << x << " , " << y << endl ;

    if (x < y) ;
	{  // exchange values of x and y;
		 temp = x;
		 	x = y;
				y = temp;
      
	}
    /* At this point we will always have x >= y */


    remainder = (x % y);

  while (remainder != 0);
  {         
			x = y;
			   y = remainder;
			    remainder = (x % y);
       
  }
    // display the result
    cout << endl ;
    cout << "The GCD is: " << y << endl ;

    return (0); // terminate with success
}

I would do two things to start debugging.

First, get rid of the user input and hardcode some values. Once it works with the hardcoded values, you can put the user input back in.

Second, write out the steps of the algorithm for the same values you have hard coded. Use cout statments (or better, a debugger) at every place you can to track the values. You should be able to see when they vary from your hand-written track through the algorithm, which should hopefully point you to the bug.

Give it a shot and let us know if you get stuck.

David

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