Can't get rid of Error lnk2001 [duplicate] - c++

This question already has answers here:
Can't set value of static object field (error LNK2001: unresolved external symbol)
(2 answers)
Closed 7 years ago.
Hey guys and gals I need some serious assistance. I have this final and its due in 2 days and some hours. However I am unable to resolve this lnk2001 error at the end of my program. Please help me I have no clue how to resolve. I left the instructors desires in the coding as comments.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
using std::cout;
using std::cin;
using std::endl;
//
//CLASS DECLARATION SECTION
//
class EmployeeClass {
public:
void ImplementCalculations(string EmployeeName, double hours, double wage);
void DisplayEmployInformation(void);
static double Addsomethingup(EmployeeClass, EmployeeClass, EmployeeClass);
string EmployeeName;
double hours;
double wage;
double basepay;
double overtime_hours;
double overtime_pay;
double overtime_extra;
static double iTotal_salaries;
static double iIndividualSalary;
static double iTotal_hours;
static double iTotal_OvertimeHours;
};
int main()
{
system("cls");
cout << "Welcome to the Employee Pay Center" << endl;
/*
These are my three employees
*/
EmployeeClass Employ1;
EmployeeClass Employ2;
EmployeeClass Employ3;
/*
Here you will prompt for the first employee’s information.
Prompt the employee name, hours worked, and the hourly wage. For each piece
of information, you will update the appropriate class member defined above.
Example of Prompts
Enter the employee name =
Enter the hours worked =
Enter his or her hourly wage =
*/
cout << "Enter the employee name." << endl;
cin >> Employ1.EmployeeName;
cout << "Enter the hours worked." << endl;
cin >> Employ1.hours;
cout << "Enter his or her hourly wage." << endl;
cin >> Employ1.wage;
/*
Here you will prompt for the second employee’s information.
Prompt the employee name, hours worked, and the hourly wage. For each piece
of information, you will update the appropriate class member defined above.
Enter the employee name =
Enter the hours worked =
Enter his or her hourly wage =
*/
cout << "Enter the employee name." << endl;
cin >> Employ2.EmployeeName;
cout << "Enter the hours worked." << endl;
cin >> Employ2.hours;
cout << "Enter his or her hourly wage." << endl;
cin >> Employ2.wage;
/*
Here you will prompt for the third employee’s information.
Prompt the employee name, hours worked, and the hourly wage. For each piece
of information, you will update the appropriate class member defined above.
Enter the employee name =
Enter the hours worked =
Enter his or her hourly wage =
*/
cout << "Enter the employee name." << endl;
cin >> Employ3.EmployeeName;
cout << "Enter the hours worked." << endl;
cin >> Employ3.hours;
cout << "Enter his or her hourly wage." << endl;
cin >> Employ3.wage;
/*
Here you will implement a function call to implement the employ calcuations
for each object defined above. You will do this for each of the three
employees or objects.
The format for this step is the following:
[(object name.function name(objectname.name, objectname.hours,
objectname.wage)] ;
*/
Employ1.ImplementCalculations(Employ1.EmployeeName, Employ1.hours,
Employ1.wage);
Employ2.ImplementCalculations(Employ2.EmployeeName, Employ2.hours,
Employ2.wage);
Employ3.ImplementCalculations(Employ3.EmployeeName, Employ3.hours,
Employ3.wage);
EmployeeClass::Addsomethingup(Employ1, Employ2, Employ3);
}
/*
This section you will send all three objects to a function that will add up
the the following information:
- Total Employee Salaries
- Total Employee Hours
- Total Overtime Hours
The format for this function is the following:
- Define a new object.
- Implement function call [objectname.functionname(object name 1, object
name 2, object name 3)]
/*
} //End of Main Function*/
void EmployeeClass::ImplementCalculations (string EmployeeName, double
hours, double wage){
//Initialize overtime variables
basepay = wage;
overtime_hours=0.0;
overtime_pay=0.0;
overtime_extra=0.0;
if (hours > 40)
{
EmployeeClass::hours = 40.0;
EmployeeClass::overtime_hours = hours - 40;
EmployeeClass::overtime_pay = wage * 1.5;
EmployeeClass::overtime_extra = overtime_hours * overtime_pay;
EmployeeClass::iIndividualSalary = overtime_extra + (wage *
EmployeeClass::hours);
/*
This section is for the basic calculations for calculating overtime pay.
- base pay = 40 hours times the hourly wage
- overtime hours = hours worked – 40
- overtime pay = hourly wage * 1.5
- overtime extra pay over 40 = overtime hours * overtime pay
- salary = overtime money over 40 hours + your base pay
*/
/*
Implement function call to output the employee information. Function is
defined below.
*/
}
else
{
EmployeeClass::hours = hours;
EmployeeClass::overtime_hours = 0.0;
EmployeeClass::overtime_pay = 0.0;
EmployeeClass::overtime_extra = 0.0;
EmployeeClass::iIndividualSalary = wage * EmployeeClass::hours;
/* Here you are going to calculate the hours less than 40 hours.
- Your base pay is = your hours worked times your wage
- Salary = your base pay
*/
/*
Implement function call to output the employee information. Function is
defined below.
*/
} // End of the else
DisplayEmployInformation();
} //End of Primary Function
void EmployeeClass::DisplayEmployInformation(void)
{
// This function displays all the employee output information.
//This is your cout statements to display the employee information:
cout << "Employee Name ............. =" << EmployeeName << endl;
cout << "Base Pay .................. =" << basepay << endl;
cout << "Hours in Overtime ......... =" << overtime_hours << endl;
cout << "Overtime Pay Amount........ =" << overtime_pay << endl;
cout << "Total Pay ................. =" << iIndividualSalary << endl;
} // END OF Display Employee Information
double EmployeeClass::Addsomethingup(EmployeeClass Employ1, EmployeeClass
Employ2, EmployeeClass Employ3)
{
Employ1.DisplayEmployInformation();
// Adds two objects of class Employee passed as
// function arguments and saves them as the calling object's data member
values.
EmployeeClass::iTotal_hours = Employ1.hours + Employ2.hours + Employ3.hours;
double myhours;
EmployeeClass::iTotal_hours = Employ1.hours + Employ2.hours + Employ3.hours;
myhours = Employ1.hours + Employ2.hours + Employ3.hours;
EmployeeClass::iTotal_OvertimeHours = Employ1.overtime_hours +
Employ2.overtime_hours + Employ3.overtime_hours;
EmployeeClass::iTotal_salaries = Employ1.iIndividualSalary +
Employ2.iIndividualSalary + Employ3.iIndividualSalary;
/*
Add the total hours for objects 1, 2, and 3.
Add the salaries for each object.
Add the total overtime hours.
*/
// Then display the information below.
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% " << endl;
cout << "%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%% " << endl;
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% " << endl;
cout << "%%%% Total Employee Salaries ..... = " << iTotal_salaries << endl;
cout << "%%%% Total Employee Hours ........ = " << iTotal_hours << endl;
cout << "%%%% Total Overtime Hours......... = " << iTotal_OvertimeHours <<
endl;
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% " << endl;
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% " << endl;
return 0;
} // End of function

Non-integral static member variables must be defined outside the class definition.
class EmployeeClass
{
// ...
static double iTotal_salaries;
static double iIndividualSalary;
static double iTotal_hours;
static double iTotal_OvertimeHours;
};
double EmployeeClass::iTotal_salaries = 0.0;
double EmployeeClass::iIndividualSalary = 0.0;
double EmployeeClass::iTotal_hours = 0.0;
double EmployeeClass::iTotal_OvertimeHours = 0.0;

Related

Issues with PrintStub() Function's ouput

Hopefully you can kind of see where Im going with this. FICA at 7.65%, FedTax at 22 or 28% depending on the amount made, and state tax at 12%. I have to make all of these functions come to an output of:
Name earned $645.00
FICA $xxx.xx
Fed $xxx.xx
State $xxx.xx
Net Pay $xxx.xx
So I believe mathematically I am there, its just setting up for success is where Iam genuinely stuck.
#include "stdafx.h"
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
int hourRate, hourWork, otHours, pay, FICA, state, fed, netpay;
void Pause()
{ string junk, extraNewLine;
cin.ignore();
cout << "Press Enter to continue ... ";
getline(cin, junk);
}
void GetPay(){
pay = (hourRate * hourWork) + (otHours * (hourRate * 1.5));
cout << pay;
}
void GetHrs(){
cout << " Hours worked? ";
cin >> hourWork;
if (hourWork > 40){
otHours = hourWork - 40;
hourWork - otHours;
}
else {
otHours = 0;
}
}
void GetRate(){
cout << "Hourly Rate? ";
cin >> hourRate;
}
void CalcFCIA(){
FICA = pay *.0765;
cout << FICA;
}
void CalcFedTax(){
if (pay > 1500){
fed = pay * .28;
}
else {
fed = pay * .22;
}
}
void CalcStateTax(){
state = pay * .12;
cout << state;
}
void PrintStub(){
cout << "FICA $" + FICA;
cout << "Fed $" + fed;
cout << "State $" + state;
netpay = pay - (FICA + fed + state);
cout << "Net Pay" + netpay;
}
int main()
{
string name;
cout << "Employee name? ";
cin >> name;
GetRate();
GetHrs();
PrintStub();
Pause();
}
My output is not displaying any integers. I am only getting:
FICA $Fed $State $Net Pay
You don't appear to be calling any of the Calc functions. Each of them will have to be called in order to have any effect.
Also, it will likely help you follow the logic to write functions that return values rather than operating on global variables -- it makes the transformations you're doing on the data much more explicit. For example, you could use functions like:
int CalcFICA(int pay){
return pay * .0765;
}

Why is my output wrong?

#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const double COUNTY_TAX = 0.02;
const double STATE_TAX = 0.04;
double totalSales;
void countyTax(double newCountyTax);
void stateTax(double newStateTax);
void total(double newTotal);
int main ()
{
cout << "Please enter the total dollar amount of sales: $";
cin >> totalSales;
countyTax(1);
stateTax(1);
total(1);
return 0;
}
void countyTax(double newCountyTax)
{
double newCountyTaxA;
newCountyTaxA = totalSales * COUNTY_TAX;
cout << "The county tax is: $" << newCountyTaxA << endl;
}
void stateTax(double newStateTax)
{
double newStateTaxA;
newStateTaxA = totalSales * STATE_TAX;
cout << "The State tax is: $" << newStateTaxA << endl;
}
void total(double newTotal)
{
double newTotalA, newStateTaxA, newCountyTaxA;
newTotalA = newStateTaxA + newCountyTaxA;
cout << "The total is: $" << newTotalA << endl;
}
hello guys! I am getting into modules in C++ and the above code compiles correctly but I can't seem to figure out why my output is wonky. I get the county tax and state tax to show properly but when the total shows I get "$nan" i was wondering if i could get some feedback on this? does it possibly have to do with the fact that within the module i am not using any global variables like i do with the totalSales and COUNTY_TAX and SALES_TAX and that when i declare newTotalA newStateTaxA newCountyTaxA they are declared but not assigned? just a tad confused here..THANKS!!!
the variables newStateTaxA and newCountyTaxA that are created and computed in countyTax and stateTax are local to those functions. Once the functions complete, those variables, and the values they contain, go away. In total you create new local variables with the same names. These have no relationship with the previously defined variables, apart from sharing names, and will not store the values previously computed in the other two functions.
The easiest solution, although not considered the most robust, is to create the two variables at the global scope, along with totalSales so that the values that they are assigned to in the two functions persist.
If you do not wish to use global variables (which I also don't care to use), you can declare them in your main function, and modify your other functions to return the calculated values:
double countyTax(double newCountyTax)
{
double newCountyTaxA;
newCountyTaxA = totalSales * COUNTY_TAX;
cout << "The county tax is: $" << newCountyTaxA << endl;
return newCountytaxA;
}
The calls in main() would then read:
newCountyTaxA = countyTax(1);
Your are doing a fundamental mistake here in the following code :
void total(double newTotal)
{
double newTotalA, newStateTaxA, newCountyTaxA;
newTotalA = newStateTaxA + newCountyTaxA;
cout << "The total is: $" << newTotalA << endl;
}
The Variables that you have created here "newStateTaxA", and "newCountyTaxA" does not have any relation to the other variables that you have created and assigned in the other methods, they are local for each method. You need to either make them global or return valued from those functions.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
const double COUNTY_TAX = 0.02;
const double STATE_TAX = 0.04;
double totalSales;
void Taxes();
int main ()
{
cout << "Please enter the total dollar amount of sales: $";
cin >> totalSales;
Taxes();
return 0;
}
void Taxes()
{
double countyTax, stateTax, total;
countyTax = totalSales * COUNTY_TAX;
stateTax = totalSales * STATE_TAX;
total = countyTax + stateTax;
cout << "The County Tax is: $" << fixed << setprecision(2) << countyTax << endl;
cout << "The State Tax is: $" << fixed << setprecision(2) << stateTax << endl;
cout << "The total is: $" << fixed << setprecision(2) << total << endl;
}
here is what I came up with. Everything works and outputs as desired. I guess now I am wondering if this type of setup with one module and multiple calculations inside it considered good programming?

[C++} Not sure if this is even possible

I'm trying to find the average net pay for a payroll program. What I'm wondering is it possible to create an array from the outputted tabular data and use the array to calculate the average? Or do I have to do this a different way? My code thus far:
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
class employee {
ifstream fin;
char employeeid[12];
char employeename[20];
char martialstatus;
int hoursworked, overtime;
double hourlyrate, overtimepay, regularpay, grosspay, taxrate, taxamount, netpay, average;
void calculateGrosspay();
void calculateTax();
void calculateNetpay();
void printHeadings();
void printData();
double findAverage();
void printAverage();
public: employee();
~employee();
void printReport(); };
employee::employee(){
fin.open(payrollData.txt);
}; //Constructor
employee::~employee() {
fin.close(); } //Destructor
void employee::calculateGrosspay() {
if (hoursworked > 40) {
overtime = hoursworked - 40;
regularpay = hoursworked * hourlyrate;
overtimepay = overtime * (hourlyrate * 1.5);
grosspay = regularpay + overtimepay;
}
else grosspay = hoursworked * hourlyrate;
} //Calculate gross pay function
void employee::calculateTax() {
taxrate = .30;
taxamount = grosspay*taxrate;
} // calculate tax function
void employee::calculateNetpay() {
netpay = grosspay - taxamount;
} //Calculate net pay
void employee::printHeadings() {
cout << setw(45) << "-Employee Payroll Report-" << endl;
cout << "____________________________________________________________" << endl;
cout << "NAME ID HW OT RT-PAY OT-PAY Gross Tax NetPay" << endl;
cout << "____________________________________________________________" << endl;
} // print table headings
void employee::printData() {
cout << setprecision(2) << setiosflags(ios::fixed | ios::showpoint);
cout << setw(6) << employeename << setw(12) << employeeid << setw(4) << hoursworked << setw(3) << overtime << setw(8) << regularpay << setw(8)
<< overtimepay << setw(8) << grosspay << setw(8) << taxamount << setw(8) << netpay << endl;
} // print data
void employee::printReport() {
int i = 0;
printHeadings();
while (fin >> employeename >> employeeid >> hoursworked >> hourlyrate) {
calculateGrosspay();
calculateTax();
calculateNetpay();
printData();
i++;
}
}
double employee::findAverage() {
return average;
}
void printAverage() {
cout << endl;
cout << "The average netpay is" << average << endl;
}
void main() {
employee.printReport();
employee.findAverage();
employee.printAverage();
}
So after the program has printed the data to the console I want to find the average of the net pays and print it to the console. Best way to do this?
You'll probably need a vector like Soren mentioned. If you're trying to get some averages for all employees (which that's what I assume you're trying to do), then let's say we're going to try and get an average for gross pay.
You'll need a vector global, like such:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
using namespace std;
vector<double> netPayList;
double globalAverage;
You'll then need to add information to the vector when GrossPay is calculated as such:
void employee::calculateNetpay()
{
netpay = grosspay - taxamount;
netPayList.push_back(netpay);
}
Finally, we can calculate the average in employee::findAverage(), we should also probably use a for loop for this as well, and in my opinion we should ditch the public function of "employee" and switch that over to a traditional function, since we are trying to get the average for many "employee" instances. The average calculation will now look like this:
double findAverage()
{
for (int iterator = 0; iterator < netPayList.size(); iterator++)
{
average += netPayList[iterator];
}
globalAverage /= netPayList.size();
return globalAverage;
}
Anyway, that's basically the gist of it. Keep working on your stuff, but also I would suggest that you title your questions better. Someone with more gumption and SO privileges than I will most likely edit your OP.

C++ Problems inputting value into class objects

I am taking a class on C++ currently and need help with a certain part of my code. It all compiles but when I go to test it I am only allowed to input one value into the window before the rest of the code advances through without input.
For reference, this is the question I am answering:
11. Payroll
Design a PayRoll class that has data members for an employee’s hourly pay rate, number of hours worked of type double . The default constructor will set the hours worked and pay rate to zero. The class must have a mutator function to set the pay rate for each employee and hours worked. The class should include accessors for both the hours worked and the rate of pay. The class should lastly have a getGross function that will return a double calculated by multiplying the hours worked by the rate
of pay.
Write a program with an array of seven PayRoll objects . The program should ask the user for the rate of pay for each employee and the number of hours each employee has worked. Be sure to include an employee claiming to work more then 60 hours per week. Print out, the array number of the employee, the hours worked, the rate of pay, and the gross pay, of all the employee's each on their own line. Set the precision for printing the doubles to two decimal places.
Input Validation: Do not accept values greater than 60 for the number of hours worked, simply have the set function set number of hours worked to 60, the maximum allowed.
My code is as follows:
#include <iostream>
#include <iomanip>
//Garrett Bartholomay
/*This program defines and implements the Payroll class.
* The class is then used in a program that calculates gross pay for an array of
* Payroll objects after accepting values for hours and pay rate from standard input*/
class Payroll
{
private:
int hoursWorked;
double payRate;
public:
Payroll();
Payroll(int, double);
void setHours(int);
void setPayRate(double);
int getHours() const;
double getPayRate() const;
double getGross()const;
};
Payroll::Payroll()
{
hoursWorked = 0;
payRate = 0.0;
}
Payroll::Payroll(int h, double r)
{
payRate = r;
hoursWorked = h;
}
void Payroll::setHours(int h)
{
hoursWorked = h;
}
void Payroll::setPayRate(double p)
{
payRate = p;
}
int Payroll::getHours() const
{
return hoursWorked;
}
double Payroll::getPayRate() const
{
return payRate;
}
double Payroll::getGross() const
{
double gross = static_cast<double>(hoursWorked) * payRate;
return gross;
}
using namespace std;
int main()
{
const int NUM_EMPLOYEES = 7;
Payroll employee[NUM_EMPLOYEES];
int pay;
int hours;
int i;
double grossPay;
for (i = 0; i < NUM_EMPLOYEES; i++)
{
cout << "Enter the # " << (i) << " employee's rate of pay per hour: ";
cin >> pay;
cout << "Enter the # " << (i) << " employee's hours worked for the week: ";
cin >> hours;
employee[i].setPayRate(pay);
employee[i].setHours(hours);
}
cout << "\n\nHere is the gross pay for each employee:\n";
cout << fixed << showpoint << setprecision(2);
for (int i = 0; i < NUM_EMPLOYEES; i++)
{
grossPay = employee[i].getGross();
cout << "The gross pay for employee # " << (i) << " is: " << grossPay << endl;
}
return 0;
}
The input resides within the loops.
Any help would be greatly appreciated.
Thanks,
G

Basic problems with writing functions. Program randomly skips to the end

I'm very new to writing functions. This is my first attempt at using call by reference parameters specifically for a homework assignment. This program should calculate how long it takes to pay off a loan based on the users input of the amount they borrow, their monthly payment and the interest rate. The program should make sure the user doesnt input a payment number less than monthly interest amount and check for negative integers etc.
The area I commented out was my first go before i attempted to turn all of that into a function that uses call by reference parameters. At that point the program ran fine and i got output within a dollar of what I was looking for. After i rewrote the program like this and tried to make it a function, the program compiles but break to the bottom cout statements after asking for the interest rate. I assume my problem is somewhere within the interest1 function where i convert the percent to a decimal but i cant figure out why it would skip the rest of the functions afterwards.
Thanks for any advice you might have on this issue or anything else that looks wrong with the program itself.
#include <iostream>
#include <fstream>
using namespace std;
double amount();
double interest1();
double pay(double amt, double interest);
void payoff(double monthly, double Minpayment,double borrow,double interest, double&
totalinterest,int& month);
int main()
{
double monthly,month,totalinterest;
cout << fixed;
cout <<showpoint;
cout <<setprecision(2);
double borrow=amount();
double interest=interest1();
double Minpayment=pay(borrow, interest);
void payoff(double monthly,double Minpayment,double borrow,double interest,double&
totalinterest,int& month);
/*
// cout << "What is the monthly payment amount? \n";
// cin >> monthly;
// if(monthly<Minpayment)
// {
// cout << "You must make payments of at least: " << Minpayment++ << endl;
// cout << "Because your monthly payment is " << Minpayment << endl;
// cout << "Dont get overwhelmed with debt!!!!!! \n";
// }
// else
//
// {
// int month = 1;
// double totalinterest=0;
// do
// {
// double Minpayment=pay(borrow, interest);
// borrow=(borrow+Minpayment)-monthly;
// totalinterest=totalinterest+Minpayment;
// month++;
// }while(borrow>monthly);
*/
cout << "Your debt will be paid off in " << month << " months \n";
cout <<" with a final payment of " << borrow << endl;
cout << "You paid " << totalinterest << " in interest. \n";
return 0;
}
double amount()
{
double borrow,amt;
do
{
cout << "How much money do you want to borrow? \n";
cin >> amt;
if(amt<1)
cout << "You must enter a positive number!";
}while(amt<1);
borrow = amt;
return borrow;
}
double interest1()
{
double rate;
do
{
cout << "What is the annual interest rate expressed as a percent? \n";
cin >> rate;
if(rate<1)
cout << "You must enter a positive number!";
}while(rate<1);
double interest = (rate/12)*.01;
return interest;
}
double pay(double borrow,double interest)
{
double Minpayment=borrow*interest;
return Minpayment;
}
void payoff(double monthly,double Minpayment,double borrow,double interest, double&
totalinterest,int& month)
{
cout << "What is the monthly payment amount? \n";
cin >> monthly;
if(monthly<Minpayment)
{
cout << "You must make payments of at least: " << Minpayment++ << endl;
cout << "Because your monthly payment is " << Minpayment << endl;
cout << "Dont get overwhelmed with debt!!!!!! \n";
}
else
{
int month = 1;
do
{
double Minpayment=pay(borrow, interest);
borrow=(borrow+Minpayment)-monthly;
double totalinterest=totalinterest+Minpayment;
month++;
}while(borrow>monthly);
}
}
When you call a function inside main, you don't need to specify parameter types anymore:
void payoff(double monthly,double Minpayment,double borrow,double interest,double&
totalinterest,int& month); //wrong
should be
payoff(monthly, Minpayment,borrow, interest, totalinterest, month);
You should put double& andint& to the function's parameter list when define the function like you did.