I'm trying to get a GCD of 2 numbers. which I can do and it works fine. however I want to return one of the values based on the function I wrote. however it returns both. I know why because I wrote it that way just to get something down. but how can I just return one value? I'm sure it's something easy, but I'm not seeing it.

#include "stdafx.h"
#include <iostream>
#include <math.h>
#include <iomanip>
using namespace std;
//---------------------------------------------------------------------------
int GCD (int, int); //prototype
int main()
{
	int n1, n2, a, b;

	cout << "This program calculates the GCD of 2 integers. " << endl;
	cout << "Please enter 2 numbers. " <<endl;
	cout << "First number is : " <<endl;
	cin >> n1;
	cout << "Second number is : " << endl;
	cin >> n2;


	cout << endl;

	a = GCD(n1,n2);
	b = GCD(n1,n2);

	cout << "The Greatest Common Divisor of the numbers you entered is: " <<endl;
	cout << a <<endl;
	cout << b << endl;


	
	     
	
return 0;
}

int GCD(int a, int b)
{
    while( 1 )
    {
        a = a % b;
		if( a == 0 )
			return b;
		b = b % a;
        if( b == 0 )
			return a;
    }

}

What do you mean "return only one of the values"? A function can only return one value, and it looks like yours is... maybe you can give an example input, expected output, and current output?

it's a greatest common divisor. you input 2 numbers and it spits out the GCD. look at the code. I want it to return 'a' or 'b' depending on which on is the GCD. it right now returns 'a' and 'b'

What do you mean "return only one of the values"? A function can only return one value, and it looks like yours is... maybe you can give an example input, expected output, and current output?

Comments
...
a = GCD(n1,n2);
	b = GCD(n1,n2);

The values of a and b will be the same because the values of n1 and n2 do not change between the function calls. There is no point in calling GCD() twice with the same parameters.

yeah I just figured that out as I was checkin to see if there was a reply to this post. I can't believe I missed something so simple. thanks. but the rest is good right. i mean it has to be since it works. but I'm trying very hard to be better at formatting and making sure things are nice and neat. thanks

a = GCD(n1,n2);
	b = GCD(n1,n2);

The values of a and b will be the same because the values of n1 and n2 do not change between the function calls. There is no point in calling GCD() twice with the same parameters.

This question has already been answered. Start a new discussion instead.