greatest common divisor need help correcting

Nov 4, 2013 at 2:47am
My instructions were to: Write a function int gcd( int a, int b); which returns the value of the greatest common divisor of the two integers a and b . You can do this either recursively or iteratively. Incorporate this function into a program that uses a testing loop. Each iteration in the loop reads two integers and then prints the value of their greatest common divisor. Note: In the case that a = b = 0, there is no greatest common divisor (since every positive integer divides 0, and there is no largest positive integer.) In such a case, you should not call the gcd function, but print "There is no gcd."

However, there a few errors in my code that I'm not sure how to fix.
I know that on lines 25, and 36 I get an "A function-definition is not allowed here before" error. There are probably a few others. Please help me fix them! Thank you so much!!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#include <iostream>
using namespace std;


int main() {

char c;
// declaration of needed variables


while ( 1 ) {

cout << "Press q to quit or anything else to continue ";
cin >> c;
cin.ignore( 256, '\n' );
if ( c == 'q' or c == 'Q' ) {
cout << "\n\nUser quits the program.\n\n";
break;
}


// testing code start here

int gcd (int, int);
int main () {
int a, b;
cout << "Enter an integer for a: ";
cin >> a;
cout << "Enter an integer for b: ";
cout>> b;
cout << "The GCD of the two integers is :" << gcd(a,b) << endl;
return 0;
}

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

if (a != 0)
{
b %= a;
}
else return b;
cout << "There is no gcd." ;
}
}
// testing code ends here

cout << "\n--------------------------\n\n";

}
return 0;
}
Last edited on Nov 4, 2013 at 2:48am
Nov 7, 2013 at 2:14am
please help!
Nov 7, 2013 at 2:51am
Did you just find someone else's gcd() code and paste it into your assignment?
Nov 7, 2013 at 5:33am
No, I was using my textbook as reference.
Nov 7, 2013 at 5:43am
In that case, you should re-read over the introductory sections about how to structure a program because you have some serious syntactical errors. If, after doing that, you still aren't sure what's wrong, post again with any ideas you have about the problem.
Topic archived. No new replies allowed.