0

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 . . .

2
Contributors
4
Replies
5
Views
7 Years
Discussion Span
Last Post by realproskater
0

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);
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?

0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.