c++if-statementerror-correction

No matter if the condition is true if always executes in c++


Can someone correct this code please.

#include <iostream>
using namespace std;

int main()
{
    int a;
    int b;
    int c;
    double d;
    double e;
    double f;
    double g;
    f = a / b;
    g = b / a;
    c = 0;
    cin >> a;
    cin >> b;
    f = a / b;
    g = b / a;
    if (a == b)
    {
        cout << a << endl;
        return 0;
    }
    else if (f == int(f))
    {
        cout << a << endl;
        return 0;
    }
start:
    while (a * b > c)
        c = c + 1;
    d = c / a;
    e = c / b;
    if (d == int(d))
        if (e == int(e))
        {
            cout << c << endl;
            return 0;
        }
        else if (d != int(d))
            goto start;
        else if (e != int(e))
            goto start;
    if (a * b <= c)
        cout << a * b << endl;
}

No matter what the

else if(f==int(f))

code is always executed. Eg. I put in 3 and 5 and even though 3/5 gives a decimal the else if is always executed and outputs 3. WHAT AM I MISSING HERE?


Solution

  • You just need to define type a and b as double not int.