Thread: Need help with tax computation table!!

  1. #1
    Registered User
    Join Date
    Sep 2011
    Posts
    3

    Exclamation 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.

  2. #2
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    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.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Location
    india
    Posts
    18
    replace int marital as char marital[50]...instead of marital==1 use strcmp() function

  4. #4
    Registered User
    Join Date
    Aug 2011
    Location
    Montreal, Quebec, Canada
    Posts
    73
    Quote Originally Posted by vikasvijayan View Post
    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.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    Perhaps
    Code:
    if ( marital_status == 1 ) {
      cout << "single"; 
    } else {
      cout << "married";
    }



    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 ) {
        }
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with C computation
    By cbong2 in forum C Programming
    Replies: 1
    Last Post: 04-06-2010, 10:00 PM
  2. direct computation
    By Tool in forum C Programming
    Replies: 36
    Last Post: 12-17-2009, 02:02 PM
  3. C++ computation not working
    By insane_alien in forum C++ Programming
    Replies: 3
    Last Post: 05-18-2005, 12:14 PM
  4. Bit Computation
    By cisokay in forum C++ Programming
    Replies: 18
    Last Post: 05-17-2005, 12:59 PM
  5. Bit Computation Program
    By cisokay in forum C++ Programming
    Replies: 6
    Last Post: 05-13-2005, 09:32 PM

Tags for this Thread