Hey I had notice that my same problem was posted about 6 months ago but remained unsolved so I'm hoping that someone has the answer to my problem. I'm writing a tax program that does the following: For single people, the standard exemption is $4,000; for married people, the standard exemption is $7,000. A person can also put up to 6% of his or her gross income in a pension plan. The tax rates are as follows:

IF the taxable income is

- Between $0 & $15,000, the tax rate is 15%
- Between $15,001 and $40,000, the tax is $2,250 plus 25% of the taxable income over $15,000
- Over $40,000, the tax is $8,460 plus 35% of the taxable income over $40,000

The user should enter:
- Marital Status
- If the martial status is "married" ask for the number of children under the age of 14
- Gross salary (If the marital status is "married" and both spouses have income, enter the combined salary
- Percentage of gross income contributed to a pension fund.

The program must consist of at least the following functions:

a. function getData: this function asks the user to enter the relevant data
b. function taxAmount: this function computes and returns the tax owed

This is the code that I currently have:

#include <iostream>
#include <string>



using namespace std;
int main ()
{
    
int getNumChildren();
double taxAmount(int,double,double,int);
    
    void getData();
    

    getData();



    return 0;
}
void getData()
{
    
    char status, answer;
    int noOfChildren;
    double salary, amtInPension;
    int numPerson, standardExemption;
    double tax;

    cout << "Please enter your Marital Status: [M]arried or [S]ingle ";
    cin >> status;
    cout << endl;

    if (status == 'm' || status == 'M')
    {    
            noOfChildren = getNumChildren();

            cout << "Do both of the spouses earn income? Please enter [Y]es or [N]o. ";
            cin >> answer;
            cout << endl;

              if (answer == 'y' || 'Y')
                {    
                    cout << "Please enter your combined salary: ";
                    cin >> salary;
                    cout << endl;
                }
        
              else if (answer == 'n' || 'N')
              {
                  cout << "Please enter the salary: ";
                  cin>> salary;
                  cout<< endl;
              }

              numPerson = 2 + noOfChildren;
    }
    else
    {
        cout << "Please enter your salary: ";
        cin >> salary;
        cout << endl;

        numPerson = 1;
    }

    cout << "Please enter your contribution to Pension fund: ";
    cin >> amtInPension;
    cout << endl;

    tax = taxAmount(numPerson, salary, amtInPension, standardExemption);
}

int getNumChildren()
{
    int children;

    cout << "Please enter number of Children under the age of 14: ";
    cin >> children;
    cout << endl;
    
    return children;
}

double taxAmount(int numPerson, double salary, double amtInPension, int standardExemption)
{
    double taxableIncome;


taxableIncome = salary - (1500.00 * numPerson) - amtInPension - standardExemption;
     return(taxableIncome);
}

many thanks to whoever checks this out and helps!

Recommended Answers

All 7 Replies

so far i can see the following mistakes...when you are calling functions you dont put the whole function definition..
also you need to declare all the functions before defining them.

int getNumChildren();

that shouldnt be like that and i dont think it is not necessary to call it again since you have already done so in "getData"

void get data();

that should be

get data();

Hey that was pretty helpful, I'm just getting a little bit confused with the getData functions :(

Sorry, Forgot to show you what I had so far...

#include <iostream>
#include <string>



using namespace std;
int main ()
{
    
int getNumChildren();
double taxAmount(int,double,double,int);
    
  

    return 0;
}

 getData()
{
    void taxAmount(int & numPerson, double & salary, double & amtInPension, int & standardExemption);
    char status, answer;
    int noOfChildren;
    double salary, amtInPension;
    int numPerson, standardExemption;
    double tax;

    cout << "Please enter your Marital Status: [M]arried or [S]ingle ";
    cin >> status;
    cout << endl;

    if (status == 'm' || status == 'M')
    {    
         noOfChildren = getNumChildren();


            cout << "Do both of the spouses earn income? Please enter [Y]es or [N]o. ";
            cin >> answer;
            cout << endl;

              if (answer == 'y' || 'Y')
                {    
                    cout << "Please enter your combined salary: ";
                    cin >> salary;
                    cout << endl;
                }
        
              else if (answer == 'n' || 'N')
              {
                  cout << "Please enter the salary: ";
                  cin>> salary;
                  cout<< endl;
              }

              numPerson = 2 + noOfChildren;
    }
    else
    {
        cout << "Please enter your salary: ";
        cin >> salary;
        cout << endl;

        numPerson = 1;
    }

    cout << "Please enter your contribution to Pension fund: ";
    cin >> amtInPension;
    cout << endl;

    tax = taxAmount(numPerson, salary, amtInPension, standardExemption);
}

int getNumChildren()
{
    int children;

    cout << "Please enter number of Children under the age of 14: ";
    cin >> children;
    cout << endl;
    
    return children;
}

double taxAmount(int numPerson, double salary, double amtInPension, int standardExemption)
{
    double taxableIncome;


taxableIncome = salary - (1500.00 * numPerson) - amtInPension - standardExemption;
     return(taxableIncome);
}
#include <iostream>
#include <string>



using namespace std;
int main ()
{
    
int getNumChildren();
double taxAmount(int,double,double,int);
    
  

    return 0;
}

 getData()
{
    void taxAmount(int & numPerson, double & salary, double & amtInPension, int & standardExemption);
    char status, answer;
    int noOfChildren;
    double salary, amtInPension;
    int numPerson, standardExemption;
    double tax;

    cout << "Please enter your Marital Status: [M]arried or [S]ingle ";
    cin >> status;
    cout << endl;

    if (status == 'm' || status == 'M')
    {    
         noOfChildren = getNumChildren();


            cout << "Do both of the spouses earn income? Please enter [Y]es or [N]o. ";
            cin >> answer;
            cout << endl;

              if (answer == 'y' || 'Y')
                {    
                    cout << "Please enter your combined salary: ";
                    cin >> salary;
                    cout << endl;
                }
        
              else if (answer == 'n' || 'N')
              {
                  cout << "Please enter the salary: ";
                  cin>> salary;
                  cout<< endl;
              }

              numPerson = 2 + noOfChildren;
    }
    else
    {
        cout << "Please enter your salary: ";
        cin >> salary;
        cout << endl;

        numPerson = 1;
    }

    cout << "Please enter your contribution to Pension fund: ";
    cin >> amtInPension;
    cout << endl;

    tax = taxAmount(numPerson, salary, amtInPension, standardExemption);
}

int getNumChildren()
{
    int children;

    cout << "Please enter number of Children under the age of 14: ";
    cin >> children;
    cout << endl;
    
    return children;
}

double taxAmount(int numPerson, double salary, double amtInPension, int standardExemption)
{
    double taxableIncome;


taxableIncome = salary - (1500.00 * numPerson) - amtInPension - standardExemption;
     return(taxableIncome);
}

I see some possible confusion about where to place function headers/declarations, where to place the functions themselves, and where to place the function calls. Function declarations should go before the main () function. The functions themselves should go after the main () function. You should not have a function or function declaration within another function. Function calls should be placed within functions.

So lines 10 and 11, which are function declarations, should be moved BEFORE line 7 (and should probably go AFTER line 6, though this often doesn't make a difference).

The intent of line 18 appears to be the start of a function itself. However, it is not. It is a function call. If it is a function call, it needs to be inside of a function. It is not. The main function stopped at line 16. Further, there is no function declaration for getData () before main() starts.

getData () looks like it should be a void function. You could change line 18 to this:

void getData ()

and you would have. You could add this line:

void getData ();

after line 6 and before line 7. Your function call would likely be from main (). If you put this line:

getData ();

after line 8 and before line 15, this would be a legal function call from main () to the function called getData (). That appears to be your intent. Line 20, like lines 10 and 11, is a function declaration, and like them, should not be inside another function. Like lines 10 and 11, move line 20 to be before line 7 and after line 6.

The function declaration in line 20 does not appear to match the function itself on line 83. Change line 20 (after moving it so it is before line 7) so that it matches the actual function on line 83.

After making those changes, see if it compiles and runs and go from there.


The function declaration in line 20 does not appear to match the function itself on line 83. Change line 20 (after moving it so it is before line 7) so that it matches the actual function on line 83.

After making those changes, see if it compiles and runs and go from there.

Actually I am a little confused about what the actual function is. I see two function declarations for taxAmount, one on line 11, one on line 20. The actual function on line 83 appears to match line 11. I don't see a function that corresponds to the function declaration on line 20. Perhaps line 20 should be deleted altogether?

Wow thanks a lot man that really helps. I did everything you said including deleting that extra line. I believe it was 20. Anyway, i just have one more request. I can't seem to get it to output the results. I tried a simple cout at the end but that doesn't work.

#include <iostream>
#include <string>



using namespace std;

void getData ();
int getNumChildren();
double taxAmount(int,double,double,int);

int main ()
{
    getData ();

    
  

    return 0;
}

 void getData ()
{
    
    char status, answer;
    int noOfChildren;
    double salary, amtInPension;
    int numPerson, standardExemption;
    double tax;

    cout << "Please enter your Marital Status: [M]arried or [S]ingle ";
    cin >> status;
    cout << endl;

    if (status == 'm' || status == 'M')
    {    
         noOfChildren = getNumChildren();


            cout << "Do both of the spouses earn income? Please enter [Y]es or [N]o. ";
            cin >> answer;
            cout << endl;

              if (answer == 'y' || 'Y')
                {    
                    cout << "Please enter your combined salary: ";
                    cin >> salary;
                    cout << endl;
                }
        
              else if (answer == 'n' || 'N')
              {
                  cout << "Please enter the salary: ";
                  cin>> salary;
                  cout<< endl;
              }

              numPerson = 2 + noOfChildren;
    }
    else
    {
        cout << "Please enter your salary: ";
        cin >> salary;
        cout << endl;

        numPerson = 1;
    }

    cout << "Please enter your contribution to Pension fund: ";
    cin >> amtInPension;
    cout << endl;

    tax = taxAmount(numPerson, salary, amtInPension, standardExemption);
}

int getNumChildren()
{
    int children;

    cout << "Please enter number of Children under the age of 14: ";
    cin >> children;
    cout << endl;
    
    return children;
}

double taxAmount(int numPerson, double salary, double amtInPension, int standardExemption)
{
    double taxableIncome;


taxableIncome = salary - (1500.00 * numPerson) - amtInPension - standardExemption;
     return(taxableIncome);
}

Wow thanks a lot man that really helps. I did everything you said including deleting that extra line. I believe it was 20. Anyway, i just have one more request. I can't seem to get it to output the results. I tried a simple cout at the end but that doesn't work.

#include <iostream>
#include <string>



using namespace std;

void getData ();
int getNumChildren();
double taxAmount(int,double,double,int);

int main ()
{
    getData ();

    
  

    return 0;
}

 void getData ()
{
    
    char status, answer;
    int noOfChildren;
    double salary, amtInPension;
    int numPerson, standardExemption;
    double tax;

    cout << "Please enter your Marital Status: [M]arried or [S]ingle ";
    cin >> status;
    cout << endl;

    if (status == 'm' || status == 'M')
    {    
         noOfChildren = getNumChildren();


            cout << "Do both of the spouses earn income? Please enter [Y]es or [N]o. ";
            cin >> answer;
            cout << endl;

              if (answer == 'y' || 'Y')
                {    
                    cout << "Please enter your combined salary: ";
                    cin >> salary;
                    cout << endl;
                }
        
              else if (answer == 'n' || 'N')
              {
                  cout << "Please enter the salary: ";
                  cin>> salary;
                  cout<< endl;
              }

              numPerson = 2 + noOfChildren;
    }
    else
    {
        cout << "Please enter your salary: ";
        cin >> salary;
        cout << endl;

        numPerson = 1;
    }

    cout << "Please enter your contribution to Pension fund: ";
    cin >> amtInPension;
    cout << endl;

    tax = taxAmount(numPerson, salary, amtInPension, standardExemption);
}

int getNumChildren()
{
    int children;

    cout << "Please enter number of Children under the age of 14: ";
    cin >> children;
    cout << endl;
    
    return children;
}

double taxAmount(int numPerson, double salary, double amtInPension, int standardExemption)
{
    double taxableIncome;


taxableIncome = salary - (1500.00 * numPerson) - amtInPension - standardExemption;
     return(taxableIncome);
}

You have a few options for displaying the results. I'm not sure where you tried to display when you say "at the end", but you could do a few things.

One, you could add this line after line 73.

cout << "You paid " << tax << " in taxes." << endl;

Two, you could change your getData () function to return a double. Instead of placing the line above after line 73, place it on line 15. After line 73, put this line:

return tax;

Change line 14 to this:

double tax = getData ();

Third option, change your getData () function to something like this:

void getData (int& numPerson, double& salary, double& amtInPension, int& standardExemption)

Rather than call the taxAmount function from getData, call it from main. To do that requires passing the variables to the getData function by reference. You would move lines 27 and 28 to the top of your main function, then pass those variables to getData. getData would set the values and pass them back to main. From main, call the taxAmount function, and display the result from main as well. The advantage of this method would be that your functions do what their names imply and no more, which is generally the strategy in C++. The disadvantage is that it would require you to change your program the most.

Regardless, the way you have it now, since getData () is a void function, it won't return any values. You are calculating tax in line 73, then immediately returning from the function without doing anything with it. tax is a local variable and thus goes out of scope after the function is over, so the contents of that variable is lost immediately.

I'd say option three is probably the preferred option among programmers since each function will do a very specific task, the task it does is well described by its name, and the main () function controls everything. However, any of the three options should work.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.