Expected initializer before "do" error message

Okay, so I am super new to programming and our assignment for my class is to write a program that calculates the greatest common divisor but the first number entered by the user must be less than the second number. I wrote my code but when I go to compile it I am given the error message "expected initializer before "do"". I have tried tweaking everything I can think of to get rid of this error message but nothing seems to work. I was hoping someone would be able to point out the error I made that is causing this. (Note: I've tried googling this but nothing I've found has been of any use).

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
#include <iostream>
#include <cmath>


using namespace std;

int gcd(int x, int y) {
    return y == 0 ? x : gcd(y, x % y);
}

int main() 

 do {

    
	 cout << "Enter first number: ";
     cin >> x;
	 cout << "Enter second number: ";
     cin >> y;
	
	 if (x < y)
		cout << "Please enter a number larger than the first number.";
			return 
    
	 if else (x > y)
       cout << "Greatest Common Divisor: " << gcd(abs(x), abs(y)) << endl;
 }
You appear to be missing the beginning and ending braces for main().

Last edited on
you could use another function other than recursive to better understand how the process goes:
1
2
3
4
5
6
7
8
9
10
11
12
int gcd(int n1, int n2)

{
    while((n1%n2)!=1)
    {
       int c=n1%n2;
       n1=n2;
       n2=c;
       cout<<c<<endl;
       
    }
}
Topic archived. No new replies allowed.