If statements help

Ok, so I'm writing a tax program and this is just the first part I'm having trouble getting the right deductions. I think it has something to do with the nested if statements. basically the problem is that every time I check status values they all run as 1 no matter what. I don't really understand what the problem is and I appreciate any help. thanks in advanced.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include<iostream>
#include<iomanip>
#include<cmath>
using namespace std;
int main()
{	
	
	int status;
	float dependents,taxdue,taxpaid, taxes, income, deduction;
	cout<<"Select filing status: "<<endl
		<<"1. Single"<<endl
		<<"2. Married Filing Seperately"<<endl
		<<"3. Married Filing Jointly"<<endl
		<<"4. Head of Household"<<endl;
	cin>>status;
	cout<<"Number of Dependents: "<<endl;
	cin>>dependents;
	if(status=1){
		
			if (dependents<=4 && dependents>=0)
				deduction=2000+(dependents*150);
			else if(dependents>4)
				deduction=2000+(150*4);
			else
				cout<<"Error invalid amount of dependents"<<endl;}
	else if(status=2){
			
			if (dependents<=4 && dependents>=0)
				deduction=1700+(dependents*125);
			else if(dependents>4)
				deduction=1700+(125*4);
			else
			cout<<"Error invalid amount of dependents"<<endl;}
							
	else if(status=3){
			if (dependents<=5 && dependents>=0)
				deduction=2300+(dependents*175);
			else if(dependents>5)
				deduction=2300+(175*5);
			else
			cout<<"Error invalid amount of dependents"<<endl;}
	
	else if(status=4){
		if (dependents<=5 && dependents>=0)
				deduction=2700+(dependents*175);
			else if(dependents>5)
				deduction=2700+(175*5);
			else
			cout<<"Error invalid amount of dependents"<<endl;}
	else
		cout<<"\nError: Invalid Entry"<<endl;
cout<<"Deduction: "<<deduction<<endl;
You should increase the verbosity of your compiler. Then it might warn about "assignment within conditional".

In other words: '=' is an assignment operator, and '==' compares for equality. Your line 18 first sets the value of 'status' to be 1 and then considers whether the return value from assignment (i.e. 1) is true or false. Non-zero values convert to true.

One useful style habit is to put constant expressions to the left side of equality comparison. 7=status is clearly an error, 7==status is not.
Wow, i feel so stupid! lol Thanks a lot man!
I'm a beginner myself so forgive me if I'm wrong but I think the 'if' statements are using the assignment operator rather than the equality operator, meaning that you are ASSIGNING the value of 1 to the status variable instead of checking to see if it is equal to 1. So instead of "if(status=1)", I think it should be "if(status==1)".
Last edited on
Topic archived. No new replies allowed.