Trying to understand the error!

Mar 11, 2018 at 11:56pm
#include <iostream>
#include <cstdlib>
#include <iomanip>

//Define constant
#define OVERTIME 40
#define FICA_TAX 6.2
#define ALLOWANCES 345.80

using namespace std;

//Function prototypes
void input(string&, double&, int&, int&, char& , double& );
double calcFunction(double&, double&, double&, char, int, double&, double);
void output(string, double, double, double, double, double);

//Main function
int main ()
{
//Declare variables
string emplname;
char marital;
int hrworked, allowance;
double hrwage, prevytdearnings, ytdearnings, currentearnings, FICAtax, taxwithheld, amtcheck,tax = 0;

//Input information
input(emplname, hrwage, hrworked, allowance, marital, prevytdearnings);

//Calculation
calcFunction(currentearnings, FICAtax, taxwithheld, marital, allowance, ytdearnings, tax);

//Calculate amount of check
amtcheck = currentearnings - FICAtax - taxwithheld;

//Calculate year-to-date earnings
ytdearnings = currentearnings + prevytdearnings;

//Calculate current earnings
if (hrworked > 40)
currentearnings = hrwage * hrworked * 1.5;
else currentearnings = hrwage * hrworked;

//Ouput information
output(emplname, currentearnings, ytdearnings, FICAtax, taxwithheld, amtcheck);

system("pause");
return 0;
}

/////////////////////////
void input(string& emplname, double& hrwage, double& hrworked, int& allowance, char& marital, double& prevytdearnings)
{
/*Pre: emplname - reference to employee's name
hrwage - reference to wage per hour
hrworked - reference to hours worked
allowance - reference to withholding allowances
marital - reference to marital status
ytdearnings - reference to previous year-to-date earnings
Post: Nothing
Purpose: Input information from user
*/
cout << "Enter the employee's name: ";
cin >> emplname;
cout << "Enter the hours wage: ";
cin >> hrwage;
cout << "Enter the hours worked: ";
cin >> hrworked;
cout << "Enter the withholding allowances: ";
cin >> allowance;
cout << "Enter the marital status: <Married: M, Single: S>";
cin >> marital;
cout << "Enter the year-to-date earnings: ";
cin >> prevytdearnings;

return;

}
/////////////////////////

//Calculation

double calcFunction(double& currentearnings, double& FICAtax, double& taxwithheld, char& marital, int& allowance, double tax, double& ytdearnings)
{
tax = 0;
/* Pre: currentearnings - reference to current making based on hours wage times hours worked
FICAtax - refernce to FICA tax
taxwithheld - reference to the amount of wages after substracting withholding allowances
marital - reference marital status
allowance - reference to withholding allowance
tax - the income tax withheld
ytdearnings - reference to year-to-date earnings
Post: total of the bill
Purpose: Calculate FICA tax, tax withheld
*/

//Calculate FICA tax

if (ytdearnings < 128700)
FICAtax = currentearnings * FICA_TAX;
else
FICAtax = 0.00;

//Calculate income tax withheld

taxwithheld = currentearnings - (ALLOWANCES * allowance);

if (marital == 'S' || marital == 's'){
if (taxwithheld < 308)
tax = 0;
if (taxwithheld > 308 && taxwithheld <= 1102)
tax = taxwithheld * 1.1;
if (taxwithheld > 1102 && taxwithheld <= 3533)
tax = 79.40 + taxwithheld * 1.12;
if (taxwithheld > 3533 && taxwithheld <= 7183)
tax = 371.12 + taxwithheld * 1.22;
if (taxwithheld > 7183 && taxwithheld <= 13433)
tax = 1174.12 + taxwithheld * 1.24;
if (taxwithheld > 13433 && taxwithheld <= 16975)
tax = 2674.12 + taxwithheld * 1.32;
if (taxwithheld > 16975 && taxwithheld <= 41975)
tax = 3807.56 + taxwithheld * 1.35;
if (taxwithheld > 41975)
tax = 12557.56 + taxwithheld * 1.37;
}
if (marital == 'M' || marital == 'm'){
if (taxwithheld < 963)
tax = 0;
if (taxwithheld > 953 && taxwithheld <= 2550)
tax = taxwithheld * 1.1;
if (taxwithheld > 2550 && taxwithheld <= 7413)
tax = taxwithheld * 1.12;
if (taxwithheld > 7413 && taxwithheld <= 14713)
tax = 742.26 + taxwithheld * 1.22;
if (taxwithheld > 14713 && taxwithheld <= 27213)
tax = 2348.26 + taxwithheld * 1.24;
if (taxwithheld > 27213 && taxwithheld <= 34296)
tax = 5348.26 + taxwithheld * 1.32;
if (taxwithheld > 34296 && taxwithheld <= 50963)
tax = 7614.82 + taxwithheld * 1.35;
if (taxwithheld > 50963)
tax = 13448.27 + taxwithheld * 1.37;
}

return tax;
}
/////////////////////////

//Output function

void output(string emplname, double currentearnings, double ytdearnings, double FICAtax, double taxwithheld, double amtcheck)
{
/*Pre: emplname - employee's name
currentearnings - current earnings
ytdearnings - year-to-date earnings
FICAtax - FICA tax
taxwithheld - income tax to be withheld
Post: Nothing
Purpose: Output information
*/
cout << setprecision(2) << fixed;
cout << emplname << endl;
cout << "Current Earnings" << setw(12) << currentearnings << endl;
cout << "Year-to-date" << setw(12) << ytdearnings << endl;
cout << "FICA tax" << setw(12) << FICAtax << endl;
cout << "Income Tax Withheld" << setw(12) << taxwithheld << endl;
cout << "Amount of check" << setw(12) << amtcheck << endl;


}


This 3 errors keep popping up when I run. Can anyone please help?
1/ "calcFunction(double&, double&, double&, char, int, double&, double)", referenced from:
2/ "input(std::__1::basic_string<char, std::__1::char_traits<char>, std::__1::allocator<char> >&, double&, int&, int&, char&, double&)", referenced from:
3/clang: error: linker command failed with exit code 1 (use -v to see invocation)





Mar 12, 2018 at 5:20am
Hi,

Try copying the function declaration, then paste it to the function definition so they are the same. It's a good practise to include variable names in the function declaration too:

1
2
3
4
5
6
void input(string& emplname, double& hrwage, int& hrworked, int& allowance, char& marital, double& prevytdearnings);

void input(string& emplname, double& hrwage, int& hrworked, int& allowance, char& marital, double& prevytdearnings) {


}


Quite a lot of the function parameters in calcFunction could be const, it means we are not going to change the value of them inside the function.


Avoid having #define statements, make them const variables instead, not global.

Also please always use code tags: www.cplusplus.com/articles/z13hAqkS/

Select your code then press the <> button on the format menu

It's a good idea to declare your variable s 1 per line, there are some situation where that can get one into trouble.

When using a double value, put digits before and after the decimal point, it helps with a certain kind of error that is hard to see:

1
2
3
4
int  a = 10;

double b = (a / 4);  // integer division, b is 2.0
double c = (a / 4.0) // c is 2.5 



if (taxwithheld < 963.0) // reinforece the idea that taxwithheld is a double

Always put braces around a block for an if statement or a loop, even if it is only 1 line. It will save you one day when you add more code:

1
2
3
if (taxwithheld < 308) {
       tax = 0;
}



Instead of magic numbers everywhere, make them variables:

1
2
3
4
5
std::vector<double> SingleTaxBrackets = {308.0, 1102.0, 3533.0, 7183.0, 13433.0, 16975.0, 41975.0};

if (taxwithheld > SingleTaxBrackets[0] && taxwithheld <= SingleTaxBrackets[1] ) {
     tax = taxwithheld * 1.1;
}


You can do the same with the tax rates ad all the other related info. Later you will learn how to combine these things together with a struct

If you are not allowed to use std::vector, then an ordinary array will do:

1
2
3
4
5
double SingleTaxBrackets[] = {308.0, 1102.0, 3533.0, 7183.0, 13433.0, 16975.0, 41975.0};

if (taxwithheld > SingleTaxBrackets[0] && taxwithheld <= SingleTaxBrackets[1] ) {
     tax = taxwithheld * 1.1;
}


Good Luck !!





Last edited on Mar 12, 2018 at 5:21am
Topic archived. No new replies allowed.