2

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?

Nae
  • 14,209
  • 7
  • 52
  • 79
LuckyFire
  • 41
  • 7
  • 1
    Please learn about and apply indentation. – Yunnosch Feb 23 '20 at 21:37
  • can you send the corrected if else part please because i am a beginner – LuckyFire Feb 23 '20 at 21:39
  • Please try `d = (1.0*c) / a;e = (1.0*c) / b;`. And similar for f and g. If that helps I will explain. – Yunnosch Feb 23 '20 at 21:40
  • Does this answer your question? [Integer division always zero](https://stackoverflow.com/questions/9455271/integer-division-always-zero) – Yksisarvinen Feb 23 '20 at 21:42
  • 2
    `f = a / b;` at this piont `a` and `b` are uninitialized, leading to undefined behavior. Before doing anything else, fix that. – Timo Feb 23 '20 at 21:42
  • first, all the division are integer division, second, what you want to do with `f == int(f)`? create a integer version of f? – Alberto Sinigaglia Feb 23 '20 at 21:42
  • `int/int` truncates its result into another `int`, you need to cast to `float` or `double` before dividing – Alan Birtles Feb 23 '20 at 21:42
  • 1
    Does this answer your question? [Why does division result in zero instead of a decimal?](https://stackoverflow.com/questions/8906722/why-does-division-result-in-zero-instead-of-a-decimal) – Alan Birtles Feb 23 '20 at 21:43
  • well on my pc if i insert "1" and "2" that else is fired.. maybe you should read about integer division and narrowing conversion – Alberto Sinigaglia Feb 23 '20 at 21:44

3 Answers3

2

These expression, though they are assigning to double variables, calculate integer divisions.

f = a / b;
g = b / a;
d = c / a;
e = c / b;

I.e. what gets assigned to the doubles are integer values.

Your if-conditions basically check for integer values and hence always evaluate to true.

In order to avoid the integer division and get actual floating point values assigned to the doubles, you need to make sure early that the compiler interprets them accordingly. E.g.:

f = (1.0*a) / b;
g = (1.0*b) / a;
d = (1.0*c) / a;
e = (1.0*c) / b;

And, as a comment points out, better always init all your variables (even if here a,b,c would be enough).

int a=1;
int b=1;
int c01;
double d=1.0;
double e=1.0;
double f=1.0;
double g=1.0;
Yunnosch
  • 26,130
  • 9
  • 42
  • 54
2

The primary error in your code leading to the problem with the if statement is the integer division. You have to cast the operands to doubles to perform floating point division:

cin >> a;
cin >> b;
f = double(a) / double(b);
g = double(b) / double(a);

There are other issues to clean up, but this is the one that leads to your question.

L. Scott Johnson
  • 4,213
  • 2
  • 17
  • 28
1

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

Mehdi
  • 717
  • 1
  • 7
  • 21