Program takes input and then...nothing

This code should preform the euclidian algorithm. Whether or not the code does what it is supposed to is not the problem. The problem is that I will input a and b and then... nothing. I tried putting a cout statement after the user inputted a and b but that did not run either.

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
  #include <iostream>
using namespace std;
int main()
{
    int a = 0, b = 0; //Initial User Input
    int x = 0, y = 0; //Adjusted User Input
    int gcd = 0; //The Greatest Common Denominator of User's numbers.
    
    cout << "Please enter two numbers: ";
    cin >> a >> b;
    
    //If a and b are the same the gcd is either a or b
    if(a==b)
        gcd=a;
    
    //Runs if a and b are not the same
    else
    {
        //Runs if a is larger than b
        if (a > b)
        {
            x = b;
            y = a;
        }
        //Runs if b is larger than a
        else
        {
            x = a;
            y = b;
        }
        
        do
        {
            if (y > x)
                y = y - x;
            
            else if (x > y)
                x = x - y;
            
        } while (x != 0 && y != 0);
        
        if ( x == 0)
            gcd = y;
        
        else if ( y == 0)
            gcd = x;
        
        cout << "The GCD is: " << gcd;
        
        
    }
}
Whether or not the code does what it is supposed to is not the problem.

Actually, that is the problem. You have an infinite loop, because your code is not doing what you intended it to do.

What happens in the loop on lines 32-40 when x becomes equal to y?
Topic archived. No new replies allowed.