Whats wrong with my code?

problem link : HS08TEST Problem - CodeChef
language: C++
my code :

#include<iostream>
#include<iomanip>
using namespace std;
int main(){
  float balance;
  int withdrawal_amount;
  cin>>withdrawal_amount;
  cin>>balance;
  if (withdrawal_amount>0 &&(withdrawal_amount%5)==0) {
          if (withdrawal_amount<=balance+0.50) {
            balance=balance-withdrawal_amount-0.50;
      cout<<fixed<<setprecision(2)<<balance;
    }
    else{
      cout<<fixed<<setprecision(2)<<balance;
    }
  }
  else{
    cout<<fixed<<setprecision(2)<<balance;
  }
  return 0;
}

I think due to precision issues your code give WA while same as your slightly changes make code AC

#include<iostream>
#include<iomanip>
using namespace std;
int main(){
  float balance;
  int withdrawal_amount;
  cin>>withdrawal_amount;
  cin>>balance;
  if (withdrawal_amount>0 &&(withdrawal_amount%5)==0) {
          if (balance - withdrawal_amount >= (0.50)) {
            balance=balance-withdrawal_amount-0.50;
            cout<<fixed<<setprecision(2)<<balance;
           }
        else{
          cout<<fixed<<setprecision(2)<<balance;
        }
   }
  else{
    cout<<fixed<<setprecision(2)<<balance;
  }
  return 0;
}

can you explain how your logic if(balance - withdrawal_amount >= (0.50))
is different from mine if(withdrawal_amount<=balance+0.50)

This is why I never work with floating points :relieved:. My Submission.

balance - withdrawal_amount >= 0.50
==> balance >= 0.50 + withdrawal_amount
==> balance - 0.50 >= withdrawal_amount
==> withdrawal_amount <= balance-0.50
but i think u’ve written balance + 0.50 instead of -

1 Like

thanks!
that was a really stupid mistake :grimacing:

Can someone help me in this. I am getting EOF error. Here is the link of submission:
https://www.codechef.com/viewsolution/34897515