-2
#include <bits/stdc++.h>

using namespace std;

int main() {
  int t;
  cin >> t;
  while (t--) {
    int x;
    int y;
    cin >> x >> y;
    if (x % 5 == 0 && x <= y) {
      cout << y - x - 0.50 << "wow"
           << "\n";

    } else {
      cout << y << "\n";
    }
  }
  return 0;
}

Input:

3
30 120.00
42 120.00
300 120.00

Output:(When y is int)

89.5wow
119.5wow
119.5wow

Here even though the second and third case should give false and run else its instead running if itself and not only that x is not being subtracted. This error is solved by making y float. But whats the underlying issue?

Output:(When y is float)

89.5wow
120
120
Biffen
  • 6,249
  • 6
  • 28
  • 36

1 Answers1

6

When you ask cin to read an int, the . is not part of an int so it does not read the .

That means the next cin>>x starts from the . which cannot be part of an int so it still doesn't read it. Same for y.

If you'd checked cin.fail() to see whether something failed, it would tell you that it had. It wasn't able to read any int because all it saw was a .

x and y are uninitialized the second and third times around the loop.

When you read floats then . is allowed to be part of reading a float so it continues reading.

user253751
  • 57,427
  • 7
  • 48
  • 90