Hi, I am trying to make this practice program to calculate pay for an employee.
I am using Xcode on a mac.
I get the following error messages when I try to run this...
error: assignment of read-only variable 'federalWithholding'
error: assignment of read-only variable 'stateWithholding'
error: assignment of read-only variable 'hospitalization'
error: assignment of read-only variable 'unionDues'
can anyone tell me why this is happening? heres the program so far...
#include <iostream>
#include <iomanip>
#include <string>
const double federalWithholding = 0.18;
const double stateWithholding = 0.045;
const double hospitalization = 25.65;
const double unionDues = 0.02;
using namespace std;
int main ()
{
string employeeInitials;
double hoursWorked,
hourlyRate,
totalWages,
totalDeductions,
netPay;
cout << "Enter employee initials: " << endl;
getline (cin,employeeInitials);
cout << "Enter Hours Worked" << endl;
cin >> hoursWorked;
cout << "Enter Hourly Rate" << endl;
cin >> hourlyRate;
cin >> totalWages = hoursWorked * hourlyRate;
federalWithholding = 0.18 * totalWages;
stateWithholding = 0.045 * totalWages;
hospitalization = 25.65;
unionDues = 0.02 * totalWages;
totalDeductions = federalWithholding + stateWithholding + hospitalization + unionDues;
netPay = totalWages - totalDeductions;
cout << fixed << showpoint << setprecision(2);
cout << "Employee Initials: " << employeeInitials << endl;
cout << "Hours Worked: " << hoursWorked << endl;
cout << "Total Wages: " << totalWages << endl;
cout << "\nDeductions";
cout << "Federal Withholding: " << federalWithholding << endl;
cout << "State Withholding: " << stateWithholding << endl;
cout << "Hospitalization: " << hospitalization << endl;
cout << "Union Dues: " << unionDues << endl;
cout << "\nTotal Deductions: " << totalDeductions << endl;
cout << "Net Pay: " << netPay << endl;
return 0;
}
const double federalWithholding = 0.18;
federalWithholding = 0.18 * totalWages;
All of the variables you are assigning to are const
which means you cannot modify them.
cin >> totalWages = hoursWorked * hourlyRate;
This will also cause a compile-time error.
Moschops,
i think i see what ur talking about...
but its hard to tell exactly...
should i leave out
federalWithholding = 0.18 * totalWages; ??
(All of the variables I am assigning to)
cin >> totalWages = hoursWorked * hourlyRate;
Yeah, i caught that one! lol
so i dont need to assign value to the constants, should i just omit the lines that try to do so?
thanks everybody for your input so far, Im going to go try to fix this thing!
lol, yeah just a bit Moschops...
but at least u helped me get rid of the errors...
now I just have to fix the calculations for totalDeductions...
heres the updated code...
#include <iostream>
#include <iomanip>
#include <string>
const double federalWithholding = 0.18;
const double stateWithholding = 0.045; // state withholding rate - named constant
const double hospitalization = 25.65; // hospitalization rate - named constant
const double unionDues = 0.02; // union dues rate - named constant
using namespace std;
int main ()
{
string employeeInitials; // character string storage
double hoursWorked, // decimal number input storage
hourlyRate, // decimal number input storage
totalWages,
totalDeductions,
netPay;
cout << "Enter employee initials: " << endl;
getline (cin,employeeInitials);
cout << "Enter Hours Worked" << endl;
cin >> hoursWorked;
cout << "Enter Hourly Rate" << endl;
cin >> hourlyRate;
totalWages = hoursWorked * hourlyRate;
totalDeductions = (federalWithholding + stateWithholding + hospitalization + unionDues);
netPay = totalWages - totalDeductions;
cout << fixed << showpoint << setprecision(2);
cout << "Employee Initials: " << employeeInitials << endl;
cout << "Hours Worked: " << hoursWorked << endl;
cout << "Hourly Rate: " << hourlyRate << endl;
cout << "Total Wages: " << totalWages << endl;
cout << "\nDeductions";
cout << "Federal Withholding: " << federalWithholding * totalWages << endl;
cout << "State Withholding: " << stateWithholding * totalWages << endl;
cout << "Hospitalization: " << hospitalization << endl;
cout << "Union Dues: " << unionDues * totalWages << endl;
cout << "\nTotal Deductions: " << totalDeductions << endl;
cout << "Net Pay: " << netPay << endl;
return 0;
}