What's wrong in the code?

Hello

I tried this problem but am unable to figure out what’s wrong with my code:

#include <iostream>
#include <iomanip>

#define EXIT_FAILURE -1
#define EXIT_SUCCESS 0

using namespace std;

int main()
{
    int X;
    float Y, fee = 0.50;

    cin >> X >> Y;

    if(X > 0 && X <= 2000)
    {
        if(Y > 0 && Y <= 2000)
        {
            if(X <= Y)
            {
                if(X % 5 == 0)
                {
                    Y = Y - ( X + 0.50 );
                    cout << setprecision(4) << Y;
                }
                else
                    cout << setprecision(4) << Y;
            }
        }
        if(X > Y)
            cout << setprecision(4) << Y;
    }

    return EXIT_SUCCESS;
}

What’s wrong with my code? Why can’t I successfully submit it?

Thanks

I would suggest you read this line again : “The cash machine will only accept the transaction if X is a multiple of 5, and Pooja’s account balance has enough cash to perform the withdrawal transaction (including bank charges)

Also there is no need to perform check on input. input will be valid and in the format given so statements like if(X > 0 && X <= 2000) are useless though they will not cause Wrong answer

1 Like

there are many bugs in ur code 1. ur setprecision not working
2.there should be a new line after every output 3.condition on Y (Y>=0)4.condition on first if statement should be(x+.50<=Y) 5.second if should be within the check condition on y
here is ur modified code which run successfully.
coding tip- there is no need to check the range of input values (such as in this case 0 < x<=2000 && 0<=y<=2000)
#include
#include
#include
#define EXIT_FAILURE -1
#define EXIT_SUCCESS 0

using namespace std;

int main()
{
int X;
float Y, fee = 0.50;

cin >> X >> Y;

if(X > 0 && X <= 2000)
{
    if(Y >= 0 && Y <= 2000)
    {
        if(X+.50 <= Y)
        {
            if(X % 5 == 0)
            {
                Y = Y - ( X + 0.50 );
                //cout << setprecision(4) << Y<<endl;
                printf("%.2f\n",Y);
            }
            else
                //cout << setprecision(4) << Y<<endl;
                printf("%.2f\n",Y);
        }
        else 
        //cout << setprecision(4) << Y<<endl;
        printf("%.2f\n",Y);
    }
    
}

return EXIT_SUCCESS;

}`

1 Like

I didn’t understand your point. I am doing the transaction only if(X % 5 == 0). Else I am just outputting initial balance.

@princerk I appreciate your effort in correcting the code but you should not have told him that (x+.50<=Y) was the error as it gives away the whole solution. Hints and tips and correction of trivial errors like ‘\n’ is ok but normally you should just give hints and let him figure out the rest.

2 Likes

Thanks!

One more question though - can you explain this statement :
printf("%.2f\n", Y)?

As I mainly use C++, I haven’t quite studied C statements like printf, scanf, etc.

using %.2f prints the variable only upto 2 decimal places. eg x=2.34345 using %f will give full value while %.2f will give 2.34.

i will take care of that next time

1 Like