# Need help with tax computation table!!

• 10-01-2011
eblanco1
Need help with tax computation table!!
Code:

```#include <iostream> #include <cstdlib> #include <math.h> #include <iomanip> using namespace std; const string FILLER = " "; int main() { int w1 = 20; int w2 = 20; int w3 = 20; int w4 = 16; //variable declarations string firstname,lastname; int marital_status; float gross_income; const float tax_rate_0 = 0.10; const float tax_rate_1 = 0.15; const float tax_rate_2 = 0.25; string single, married;   //Prompt the user to enter their name. cout << "Please enter your full name: "; cin >> firstname >> lastname; cout <<endl; //2. Enter marital status. cout << "Please enter your Marital Status: 1 for Single or 2 for Married "; cin >> marital_status; cout <<endl; //3. Enter gross income. cout << "Please enter the amount of gross income: "; cin >> gross_income; cout <<endl; cout <<endl; cout << setprecision(2) << fixed;  // set numeric output cout << setw(w1) << left << "Taxpayer" << FILLER         << setw(w2) << left << "Marital" << FILLER         << setw(w3) << left << "Income" << FILLER         << setw(w4) << left << "Tax Due" << FILLER; cout << setw(w1) << left << "" << FILLER         << setw(w2) << left << "Status" << FILLER         << setw(w3) << left << "Earned" << FILLER         << setw(w4) << left << "" << FILLER; cout<< setw(w1) << left <<  "________________________________________________________________________" << FILLER<<endl;         cout << setw(w1) << left << lastname     << setw(w2-18) << right << marital_status     << setw(w3+1) << right << "\$" << gross_income;     // Calculate tax-single part. if (marital_status==1 && gross_income <= 8000 )       cout<< setw(w4-1)<<"\$"<<((gross_income-0)*tax_rate_0); else {     if (marital_status==1 && gross_income <= 32000){     cout<< setw(w4-1)<<"\$"<<((gross_income-8000)*tax_rate_1+ 800); }     else {     if (marital_status==1 && gross_income > 32000){     cout << setw(w4-1)<<"\$" <<((gross_income-32000)*tax_rate_2 + 4400);}     }     } //Calculate tax for married part. if (marital_status== 2 && gross_income <= 16000){       cout << setw(w4-1)<<"\$" <<((gross_income-0)*tax_rate_0);} else {     if (marital_status==2 && gross_income <= 64000){     cout << setw(w4-1)<<"\$" <<((gross_income-16000)*tax_rate_1+ 1600); } else {     if (marital_status==2 && gross_income > 64000){     cout << setw(w4-1)<<"\$" <<((gross_income-64000)*tax_rate_2+ 8800 );}     }     } cout<<endl; cout<< setw(w1) << left << firstname <<endl; cout<<endl;        system("pause"); return 0; }```

This is my code.I need help figuring out how to make the marital status to be single or married instead of 1 or 2 on the table. If you see any error feel free to correct me as I'm learning on my own.
• 10-01-2011
Alexandre
What's the problem with what you have ? If you absolutely need it to be either "single" or "married" then you could have an enum or use strings but in my opinion that would be silly. Either use an int or a bool.
• 10-03-2011
vikasvijayan
replace int marital as char marital[50]...instead of marital==1 use strcmp() function
• 10-03-2011
Alexandre
Quote:

Originally Posted by vikasvijayan
replace int marital as char marital[50]...instead of marital==1 use strcmp() function

That's what you would have said about C. In C++ you would use an std::string and == operator.
• 10-04-2011
Salem
Perhaps
Code:

```if ( marital_status == 1 ) {   cout << "single"; } else {   cout << "married"; }```

Quote:

Code:

```if (marital_status== 2 && gross_income <= 16000){       cout << setw(w4-1)<<"\$" <<((gross_income-0)*tax_rate_0);} else {     if (marital_status==2 && gross_income <= 64000){```

When you find yourself repeating the same conditions, then extract them into common code.
Code:

```if ( marital_status== 2 ) {     if (  gross_income <= 16000 ) {     } else     if ( gross_income <= 64000 ) {     } }```