#basic if statement doubt

33 messages ยท Page 1 of 1 (latest)

hasty berryBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

stark hamlet
#
#include<iostream>
#include<cmath>

using namespace std;
    
    
int main(){

float income; cin >> income;
float tax,tax1, tax2, tax3 ;
if (income <= 180000) tax = 0;
if (income <= 500000)
tax2 = (income - 180000) * 0.1;
if(income <= 800000)
tax2 = (income - 500000) * 0.2 + 32000; 
else tax3 = (income - 800000)* 0.3 + 92000;
cout<< tax << ":  "<< tax1 << ":  "<< tax2 << ":  "<< tax3 << endl;
}





brazen delta
#

I reccomend stepping trough your code with a debugger

karmic mountainBOT
# stark hamlet ```cpp #include<iostream> #include<cmath> using namespace std; int ma...
#include <cmath>
#include <iostream>

using namespace std;

int main()
{

    float income;
    cin >> income;
    float tax, tax1, tax2, tax3;
    if (income <= 180000)
        tax = 0;
    if (income <= 500000)
        tax2 = (income - 180000) * 0.1;
    if (income <= 800000)
        tax2 = (income - 500000) * 0.2 + 32000;
    else
        tax3 = (income - 800000) * 0.3 + 92000;
    cout << tax << ":  " << tax1 << ":  " << tax2 << ":  " << tax3 << endl;
}

```Requested by: dragonslayer0531
stark hamlet
#

don't follow the conditions/code, i have just changed it a bit

safe vessel
#

also, format your code ๐Ÿ˜‰

stark hamlet
#

it's just when I enter 50000 the result is
0: 1.64886e-37: -58000: 2.23592e-37

#

and i'm confused here why the tax2 is like that?

and there's also this ambiguity, that if tax3 condition isn't even reaching how does tax3 somehow get printed

stark hamlet
#

but it's error free as such

brazen delta
#

well step trough the code with a debugger :)

#

it's genuinly insanely helpfull

stark hamlet
safe vessel
#

if there is no error, what is your question

stark hamlet
#

i just want to know how is tax3 somehow getting calculated if it's an else

safe vessel
#

step through with a debugger and find out why the else is executed

brazen delta
#

yes, use a debugger to see whats happening :)

stark hamlet
#

isn't that statement disregarded for 50000

brazen delta
#

and see if it actually happens as you expect

#

also check any warnings you might have

stark hamlet
brazen delta
stark hamlet
brazen delta
stark hamlet
#

like we can see if we put 50000 the value should be integer in first 3 cases but it's of some e power here

brazen delta
#

it's an insanely usefull tool

brazen delta
safe vessel
#

Please, use a debugger. It will let you walk through your code, step by step, learn see what's happening. What branches are being taken, what values things are being set to, etc

stark hamlet
#

are yaar. that's not, ok leave it for now

brazen delta
#

you know there is a real important lesson we are trying to give here right :)

#

it's literally the most important tool to have