For some reason i cant get my GCD function to return anything in my main function someone help?

#include<iostream>
using namespace std;

int firstanswer;
int secondanswer;
int a;
int b;

int gcd (int a,int b)
{
	int t;
	while ( b != 0);
	{
	t = b;
	b = a % b;
	a = t;
	}
	return a;
}


int main()
{
	cout << "Please enter your first integer: ";
	cin >> firstanswer;
	cout << "Now please enter your second integer: ";
	cin >> secondanswer;

	cout << "The GCD of " << endl;
	cout << firstanswer << endl;
	cout << "and" << endl;
	cout << secondanswer << endl;
	cout << "is" << endl;
	cout << gcd(firstanswer, secondanswer) << endl;

	return 0;
}

HERE IS MY OUTPUT:
Please enter your first integer: 10
Now please enter your second integer: 5
The GCD of
10
and
5
is
^CPress any key to continue . . .

Recommended Answers

All 4 Replies

For starters, why are firstanswer and secondanser global? They should be local to main(). Also, the global a and b are of no value to you and should not exist, get rid of them.

Your loop is not built correctly. You have an infinite loop. What's wrong with this line:

while ( b != 0);

yes i see that i have an infinite loop i am looking for a way to fix it, do you have any idea on how to clean it up alittle bit?

Get rid of the semi-colon. When you put a semi-colon in that location it messes up your logic.

yeah thank you i figured it out, thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.