C++ Object Function Error Message - c++

Alright So I finished my program that is basically just calculating up hours worked for 3 employees, nothing too complicated. However, now that I am finished I get two error messages. The first says:
expression must be a modifiable lvalue.
This refers to the use of x.hours under the Addsomethingup function. The second error message says:
'=': left operand must be l-value.
This also says that it has something to do with that iTotal_hours line under the Addsomethingup function. I am not too familiar with Classes using C++ so if anyone can offer some advice I would really appreciate it.
#include "stdafx.h"
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class EmployeeClass {
public:
string EmployeeName;
int hours;
float wage;
float basepay;
float salary;
int overtime_hours;
float overtime_pay;
float overtime_extra;
float iTotal_salaries;
float iIndividualSalary;
int iTotal_hours;
int iTotal_OvertimeHours;
void ImplementCalculations() {
overtime_hours = 0;
overtime_pay = 0;
overtime_extra = 0;
if (hours > 40) {
overtime_hours = hours - 40;
}
else {
overtime_hours = 0;
}
basepay = 40 * wage;
overtime_pay = wage * 1.5;
overtime_extra = overtime_pay * overtime_hours;
salary = overtime_extra + basepay;
}
void DisplayEmployInformation(void) {
cout << "Employee Name ............. = " << EmployeeName << endl <<
"Base Pay .................. = " << basepay << endl <<
"Hours in Overtime ......... = " << overtime_hours << endl <<
"Overtime Pay Amount........ = " << overtime_pay << endl <<
"Total Pay ................. = " << salary << endl;
}
void Addsomethingup(EmployeeClass x, EmployeeClass y, EmployeeClass z) {
iTotal_salaries = 0;
iTotal_hours = 0;
iTotal_OvertimeHours = 0;
iTotal_salaries = x.wage + y.wage + z.wage;
iTotal_hours = x.hours + y.hours = z.hours;
iTotal_OvertimeHours = x.overtime_hours + y.overtime_hours + z.overtime_hours;
cout << " %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" <<
"%%%% EMPLOYEE SUMMARY DATA%%%%%%%%%%%%%%%%%%%%%%%\n" <<
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" <<
"%%%% Total Employee Salaries ..... = " << iTotal_salaries << endl <<
"%%%% Total Employee Hours ........ = " << iTotal_hours << endl <<
"%%%% Total Overtime Hours......... = " << iTotal_OvertimeHours << endl <<
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%\n" <<
"%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
}
};
int main()
{
system("cls");
cout << "\nWelcome to the Employee Pay Center\n\n";
EmployeeClass firstEmployee;
EmployeeClass secondEmployee;
EmployeeClass thirdEmployee;
EmployeeClass totalEmployee;
cout << "Please enter the first Employees name: \n"; //First employees input
cin >> firstEmployee.EmployeeName;
cout << "Please enter " << firstEmployee.EmployeeName << "'s hours worked: \n";
cin >> firstEmployee.hours;
cout << "Please enter " << firstEmployee.EmployeeName << "'s hourly wage; \n\n";
cin >> firstEmployee.wage;
cout << "Please enter the second Employees name: \n"; //Second emlpoyee input
cin >> secondEmployee.EmployeeName;
cout << "Please enter " << secondEmployee.EmployeeName << "'s hours worked: \n";
cin >> secondEmployee.hours;
cout << "Please enter " << secondEmployee.EmployeeName << "'s hourly wage; \n\n";
cin >> secondEmployee.wage;
cout << "Please enter the third Employees name: \n"; //Third employees input
cin >> thirdEmployee.EmployeeName;
cout << "Please enter " << thirdEmployee.EmployeeName << "'s hours worked: \n";
cin >> thirdEmployee.hours;
cout << "Please enter " << thirdEmployee.EmployeeName << "'s hourly wage; \n\n";
cin >> thirdEmployee.wage;
firstEmployee.ImplementCalculations();
secondEmployee.ImplementCalculations();
thirdEmployee.ImplementCalculations();
totalEmployee.Addsomethingup(firstEmployee, secondEmployee, thirdEmployee);
}

Your issue is with
iTotal_hours = x.hours + y.hours = z.hours;
operator precendece dictates that x.hours + y.hours happens before y.hours = z.hours so what is actually going on is
iTotal_hours = (x.hours + y.hours) = z.hours;
Which will not work as (x.hours + y.hours) creates a temporary object that cannot be assigned to.
I think what you meant to have was a + and not an = which would make it
iTotal_hours = x.hours + y.hours + z.hours;
which is consistent with what you did for iTotal_salaries and iTotal_OvertimeHours

Related

Salary with vector array to get sum of employee gross pay

I am to create an application that takes at least 5 employees i.d, names, pay rate, and hours. And then I am to add the pay rate and the hours together to show the gross pay for each employee at the end of the initial inquiries. I am stuck on how to add it in the vector please help!
** Here is the assignment that our instructor gave us **
http://itweb.fvtc.edu/ag/?u=3&f=cpp-assignment4
I've added a vector and added all the essential information for the employee
#include <iostream>
#include <conio.h>
#include <string>
#include <vector>
using namespace std;
struct Employee
{
int id;
string firstName;
string lastName;
float payRate;
int hours;
};
int main()
{
/*========= other way of adding employee information ==========*/
/*const int NUM_EMPLOYEE = 5;
Employee employee[NUM_EMPLOYEE];
for (int i = 0; i < 5; i++)
{
cout << "ID of employee " << (i + 1) << ": ";
cin >> employee[i].id;
cout << "First Name of employee " << (i + 1) << ": ";
cin >> employee[i].firstName;
cout << "Last Name of employee " << (i + 1) << ": ";
cin >> employee[i].lastName;
cout << "Pay rate for employee " << (i + 1) << ": ";
cin >> employee[i].payRate;
cout << "Hours worked " << (i + 1) << ": ";
cin >> employee[i].hours;
}*/
/*========= End of other way of adding employee information ==========*/
vector<Employee> employees;
char Another = 'y';
while (Another == 'y' || Another == 'Y')
{
Employee e;
cout << "Enter employee ID: \n";
cin >> e.id;
cout << "Enter employee first name: \n";
cin >> e.firstName;
cout << "Enter employee last name: \n";
cin >> e.lastName;
cout << "Enter employee pay rate: \n";
cin >> e.payRate;
cout << "Enter employee hours worked: \n";
cin >> e.hours;
employees.push_back(e);
cout << "Another? (y/n): \n";
cin >> Another;
}
float sum = 0;
vector<Employee>::iterator it = employees.begin();
for (; it != employees.end(); it++)
{
cout << "ID of employee: \n" << it->id << ": \n"
<< "Employees name: \n" << it->firstName << " " << it->lastName << ": \n"
<< "Employee pay rate: \n" << it->payRate << ": \n"
<< "Employee hours worked: \n" << it->hours << "\n";
}
float avg = sum / employees.size();
Employee e;
/*cout << " ID of employees: \n" << e.id;
cout << " Name of employees: \n" << e.firstName << " " <<
e.lastName;*/
cout << "Gross pay of employees: \n" << avg;
_getch();
return 0;
}
Show Id, names, and gross pay of all employees to user
vector<Employee> employees;
char Another = 'y';
while (Another == 'y' || Another == 'Y')
{
Employee e;
cout << "Enter employee ID: \n";
cin >> e.id;
cout << "Enter employee first name: \n";
cin >> e.firstName;
cout << "Enter employee last name: \n";
cin >> e.lastName;
cout << "Enter employee pay rate: \n";
cin >> e.payRate;
cout << "Enter employee hours worked: \n";
cin >> e.hours;
employees.push_back(e);
cout << "Another? (y/n): \n";
cin >> Another;
}
float sum = 0;
vector<Employee>::iterator it = employees.begin();
cout << "ID" << "\t" << "First Name" << "\t" << "Last Name" << "\t" << "Pay rate" << "\t" << "Hours" << "\t" << "Gross Pay" << "\n" ;
for (; it != employees.end(); it++)
{
float grossPay = it->payRate * it->hours;
cout << it->id << "\t" << it->firstName << "\t\t" << it->lastName << "\t\t" << it->payRate << "\t\t"
<< it->hours << "$" << "\t" << grossPay << "\n";
sum += grossPay;
}
cout << "The gross pay for all employee is: " << "$" << sum;
_getch();
return 0;
}
//This is what i got so far. But if I want to make the application ask the user to input how many employee they want to enter into the database how do i do that? would it be like this?
int EMPLOYEE_NUM[][] // ??

Calculations no processing C++

I'm making a simple payroll calculator and for some reason my "taxes" aren't calculating. When I run the program they just show $0 in the output. Any idea whats causing this? I commented "// Not calculating. Shows $0" on the lines that aren't calculating.
#include <iostream>
using namespace std;
int main()
// Parameters: None
// Returns: Zero
// Calls: None
{
int const hourly_wage = 16.78, overtime_rate = 25.17, social_security = 0.06, federal_income = 0.14, state_income = 0.05, union_dues = 10;
int gross_pay, hours_worked, number_of_dependents, ss_withheld, federal_withheld, state_withheld, net_pay, insurance, overtime_pay, again;
cout << "This program will calculate payroll for an employee." << endl;
do
{
cout << "\n\nEnter the number of hours the employee worked: " << endl;
cin >> hours_worked;
if (hours_worked > 40) {
overtime_pay = (hours_worked - 40) * overtime_rate;
gross_pay = (40 * hourly_wage) + overtime_pay;
}
else {
gross_pay = hours_worked * hourly_wage;
}
cout << "\n\nEnter the number of dependents for employee: " << endl;
cin >> number_of_dependents;
if (number_of_dependents >= 3) {
insurance = 35;
}
else {
insurance = 0;
}
// Payroll Calculation
ss_withheld = gross_pay * social_security;
federal_withheld = gross_pay * federal_income;
state_withheld = gross_pay * state_income;
net_pay = gross_pay - (ss_withheld + federal_withheld + state_withheld + insurance + union_dues);
cout << "\n\nGross Pay: $" << gross_pay << endl;
cout << "Social Security Withhholding: $" << ss_withheld << endl; // Not calculating. Shows $0
cout << "Federal Withholding: $" << federal_withheld << endl; // Not calculating. Shows $0
cout << "State Withholding: $" << state_withheld << endl; // Not calculating. Shows $0
cout << "Union Dues: $" << union_dues << endl;
cout << "Insurance Deduction: $" << insurance << endl;
cout << "Net Pay: $" << net_pay << endl;
cout << "\nDo you want to do another employee(Y/N)? ";
cin >> again;
} while (again == 'y' || again == 'Y');
cout << "\nHope they enjoy the money!" << endl;
return 0;
}
It looks like you are creating int Const for double values. ie.
#include "stdafx.h";
#include <iostream>
using namespace std;
int main()
// Parameters: None
// Returns: Zero
// Calls: None
{
int const union_dues = 10;
double const hourly_wage = 16.78, overtime_rate = 25.17, social_security = 0.06, federal_income = 0.14, state_income = 0.05;
double gross_pay, hours_worked, number_of_dependents, ss_withheld, federal_withheld, state_withheld, net_pay, insurance, overtime_pay, again;
cout << "This program will calculate payroll for an employee." << endl;
do {
cout << "\n\nEnter the number of hours the employee worked: " << endl;
cin >> hours_worked;
if (hours_worked > 40) {
overtime_pay = (hours_worked - 40) * overtime_rate;
gross_pay = (40 * hourly_wage) + overtime_pay;
}
else {
gross_pay = hours_worked * hourly_wage;
}
cout << "\n\nEnter the number of dependents for employee: " << endl;
cin >> number_of_dependents;
if (number_of_dependents >= 3) {
insurance = 35;
}
else {
insurance = 0;
}
// Payroll Calculation
ss_withheld = gross_pay * social_security;
federal_withheld = gross_pay * federal_income;
state_withheld = gross_pay * state_income;
net_pay = gross_pay - (ss_withheld + federal_withheld + state_withheld + insurance + union_dues);
cout << "\n\nGross Pay: $" << gross_pay << endl;
cout << "Social Security Withhholding: $" << ss_withheld << endl; // Not calculating. Shows $0
cout << "Federal Withholding: $" << federal_withheld << endl; // Not calculating. Shows $0
cout << "State Withholding: $" << state_withheld << endl; // Not calculating. Shows $0
cout << "Union Dues: $" << union_dues << endl;
cout << "Insurance Deduction: $" << insurance << endl;
cout << "Net Pay: $" << net_pay << endl;
cout << "\nDo you want to do another employee(Y/N)? ";
cin >> again;
} while (again == 'y' || again == 'Y');
cout << "\nHope they enjoy the money!" << endl;
return 0;
}

Code WORKS Math DOESN'T

My code compiles nicely, but the math formulas that I am using aren't providing the right outcome. I need to calculate the balance, withdrawn, and interest for all 3 months. I am also required to validate user's input. For these purposes I am using nested loops. Please let me know if you spot my mistake. Thank you lovely people!
cout << "Please enter the starting balance: ";
cin >> startBalance;
cout << "Please enter the annual interest rate: ";
cin >> annualInterest;
for (int month = 1; month <= 3; month++) {
do {
cout << setprecision(5);
cout << "Please enter the total amount deposited on month " << month << ": ";
cin >> balance;
if (balance <0) {
goodChoice = false;
cout << "\n\t***ERROR " << balance << " ***";
cout << "*** Choice must be positive***\n" << endl;
}
else {
goodChoice = true;
}
startBalance += balance; //need to calculate the balance for all 3 months
} while (!goodChoice);
do {
cout << setprecision(5);
cout << "Please enter the total amount withdrawn on " << month << ": ";
cin >> withdrawn;
if ( (withdrawn <0) || (withdrawn > startBalance) ) {
goodChoice = false;
cout << "***ERROR " << withdrawn << " ***";
cout << "*** Choice must be positive or greater than the balance***" << endl;
}
else {
goodChoice = true;
}
finalWithdrawn += withdrawn; // the total amount of withdrawn
finalBalance = startBalance - withdrawn;
monthInterest = ((startBalance + finalBalance) / 2) * (annualInterest / 12);
totalInterest += monthInterest; //total interest for all 3 months
endBalance = monthInterest + finalBalance;
} while (!goodChoice);
}
cout << "Total Deposit: " << endBalance << endl;
cout << "Total Withdrawn: " << finalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << endBalance << endl;
You have a lot of extra variables defined which aren't needed. Also, your interest rate may have been in percentage instead of a decimal number, i.e. 10% = 0.1. Also, your monthInterest is taking an average then applying interest. I wrote this up and it seems to work.
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
float totalDeposit = 0.0f;
float totalWithdrawn = 0.0f;
float totalInterest = 0.0f;
float totalBalance = 0.0f;
float interestRate = 0.0f;
setprecision(5); //?
cout << "Enter starting balance: ";
cin >> totalBalance;
cout << "Enter annual interest rate (%): ";
cin >> interestRate;
// Convert to monthly and non-percent; i.e. 10% = 0.1 = 10 / 100
interestRate = interestRate / 100.0f / 12.0f;
for (int month = 1; month <= 3; month++)
{
float deposit = -1.0; // Default to an error state
while (deposit < 0.0)
{
cout << "Enter total deposited in month " << month << ": ";
cin >> deposit;
if (deposit < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
float withdrawn = -1.0f; // Default to an error state
while (withdrawn < 0.0f)
{
cout << "Enter total withdrawn in month " << month << ": ";
cin >> withdrawn;
if (withdrawn < 0.0f)
{
cout << "ERROR: Invalid amount" << endl;
continue;
}
}
totalDeposit += deposit;
totalWithdrawn += withdrawn;
totalBalance = totalBalance + deposit - withdrawn;
totalBalance += totalBalance * interestRate;
totalInterest += totalBalance * interestRate;
}
cout << "Total Deposit: " << totalDeposit << endl;
cout << "Total Withdrawn: " << totalWithdrawn << endl;
cout << "Total Interest: " << totalInterest << endl;
cout << "Final Balance: " << totalBalance << endl;
int wait; // Pause so console window doesn't close. Delete this line.
cin >> wait;
return 0;
}

Confused on uninitialized local variable being used when initialized?

I am getting an uninitialized Local variable error when I do believe I have already initialized. The error reads uninitialized local variable wk1 being used (It's wk1-wk5).
Here is the code:
#include <iostream>
using namespace std;
const double tax = 0.14;
int main()
{
int wk1,wk2,wk3,wk4,wk5;
wk1,wk2,wk3,wk4,wk5 = 0;
int thours = wk1 + wk2 + wk3 + wk4 + wk5; <------------ This is the error line.
thours = 0;
double payrate;
payrate = 0;
double gross = thours * payrate;
double taxes = tax * gross;
double net = gross - taxes;
double clothes = 0.10 * net;
double supplies = 0.10 * net;
double remaining = net - clothes - supplies;
double bonds = 0.25 * remaining;
double pbonds = 0.50 * bonds;
bonds = 0;
gross = 0;
net = 0;
clothes = 0;
supplies = 0;
remaining = 0;
cout << "Please enter the payrate for employee." << endl;
cin >> payrate;
payrate = 0;
cout << "Please enter employee's total hours for week one:" << endl;
cin >> wk1;
wk1 = 0;
cout << "Please enter employee's total hours for week two:" << endl;
cin >> wk2;
wk2 = 0;
cout << "Please enter employee's total hours for week three:" << endl;
cin >> wk3;
wk3 = 0;
cout << "Please enter employee's total hours for week four:" << endl;
cin >> wk4;
wk4 = 0;
cout << "Please enter employee's total hours for week five:" << endl;
cin >> wk5;
wk5 = 0;
cout << "Here is income before taxes: " << gross << endl;
cout << "Here is income after taxes: " << net << endl;
cout << "Here is clothes and accesories: " << clothes << endl;
cout << "Here is School supplies: " << supplies << endl;
cout << "Here is personal bonds: " << bonds << endl;
cout << "Here is parents bonds: " << pbonds << endl;
return 0;
}
wk1,wk2,wk3,wk4,wk5 = 0;
This line is a comma operator expression, which is equivalent to:
wk5 = 0;
Because expressions like wk1 has no side effect. Only the variable wk5 has been assigned a value, the other variables are still uninitialized. You can do:
wk1 = wk2 = wk3 = wk4 = wk5 = 0;

Having a hard time my code

I am taking a C++ class and I have to build a payroll system. I am having a hard time trying to figure out what is wrong with my code. I can get the employees hours to produce but I have an new issue with my code now. I thought it was correct, but guess not. Now the new problem is that I can get the employees hours to produce but when I do my code wants to multiple my overtime hours times 3 and produce an output of the person who has the overtime hours twice.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//
//CLASS DECLARATION SECTION
//
class EmployeeClass {
public:
void ImplementCalculations(string EmployeeName, int hours, float wage);
void DisplayEmployInformation(void);
void Addsomethingup (EmployeeClass Emp1, EmployeeClass Emp2, EmployeeClass Emp3);
string EmployeeName;
int hours ;
float wage ;
float basepay ;
int overtime_hours ;
float overtime_pay ;
float overtime_extra ;
float iTotal_salaries ;
float iIndividualSalary ;
int iTotal_hours ;
int iTotal_OvertimeHours ;
};
int main()
{ system("cls");
cout << "\nWelcome to Data Max Inc. Employee Pay Center\n\n" ;
EmployeeClass Emp1;
EmployeeClass Emp2;
EmployeeClass Emp3;
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
cout << "\n\nEnter the first employee's first name = ";
cin >> Emp1.EmployeeName;
cout << "\n\nEnter the hours worked = ";
cin >> Emp1.hours;
cout << "\n\nEnter employee's hourly wage = ";
cin >> Emp1.wage;
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
cout << "\n\nEnter the second employee's first name = ";
cin >> Emp2.EmployeeName;
cout << "\n\nEnter the hours worked = ";
cin >> Emp2.hours;
cout << "\n\nEnter employee's hourly wage = ";
cin >> Emp2.wage;
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
cout << "\n\nEnter the third employee's first name = ";
cin >> Emp3.EmployeeName;
cout << "\n\nEnter the hours worked = ";
cin >> Emp3.hours;
cout << "\n\nEnter employee's hourly wage = ";
cin >> Emp3.wage;
cout << "%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%" << endl;
cout << endl;
Emp1.ImplementCalculations(Emp1.EmployeeName, Emp1.hours, Emp1.wage);
Emp2.ImplementCalculations(Emp2.EmployeeName, Emp2.hours, Emp2.wage);
Emp3.ImplementCalculations(Emp3.EmployeeName, Emp3.hours, Emp3.wage);
cin.get();
return 0;
} //End of Main Function
void EmployeeClass::ImplementCalculations (string employeeFirstName, int hours, float wage){
//Initialize overtime variables
overtime_hours=0;
overtime_pay=0;
overtime_extra=0;
if (hours > 40)
{
basepay = 40 * wage;
overtime_hours = hours - 40;
overtime_pay = wage * 1.5;
overtime_extra = overtime_hours * overtime_pay;
iIndividualSalary = overtime_extra + basepay;
DisplayEmployInformation();
} // if (hours > 40)
else
{
basepay = hours * wage;
iIndividualSalary = basepay;
} // End of the else
DisplayEmployInformation();
} //End of Primary Function
void EmployeeClass::DisplayEmployInformation () {
// This function displays all the employee output information.
cout << "\n\n";
cout << "Employee First Name ............. = " << EmployeeName << endl;
cout << "Base Pay ........................ = " << basepay << endl;
cout << "Hours in Overtime ............... = " << overtime_hours << endl;
cout << "Overtime Pay Amout............... = " << overtime_extra << endl;
cout << "Total Pay ....................... = " << iIndividualSalary << endl;
} // END OF Display Employee Information
void EmployeeClass::Addsomethingup (EmployeeClass Emp1, EmployeeClass Emp2, EmployeeClass Emp3){
iTotal_salaries = 0;
iTotal_hours = 0;
iTotal_OvertimeHours = 0;
iTotal_hours = Emp1.hours + Emp2.hours + Emp3.hours;
iTotal_salaries = iIndividualSalary + iIndividualSalary + iIndividualSalary;
iTotal_OvertimeHours = overtime_hours + overtime_hours + overtime_hours;
cout << "\n\n";
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;
} // End of function
You aren't calling Addsomethingup anywhere. You probably also want this to be a static method. If you haven't learned what those are yet, don't worry.
At the end of your main function, but before cin.get(), try adding:
Emp1.Addsomethingup(Emp1, Emp2, Emp3);