I'm completely new to C++ and trying to figure this stuff out I had posted a topic a couple days ago. After many frustrations and headaches, I deiced to start over. This is what I got:

#include<iostream>
#include<iomanip>
using namespace std;

double ot = 0;
double getHoursWorked();
double getPayRate();
double calcTax(double grossPay, double taxAmt);
double grossPay();
double calcIns(double insRate, double grossPay, double insAmt);
double getInsRate(double insuranceType, double insRate);
double calcBonus(double grossPay, double bonusPay);
double calcGross(double&, double);
void displayData(double, double, double, double, double, double, double );

int main()
{
	//declare variables
	double hours = 0.0, rate = 0.0, gross = 0.0, bonus = 0.0, tax = 0.0, insur = 0.0;

	

	//get and calculate hours and pay
	hours = getHoursWorked();
	rate = getPayRate();
	insur = getInsRate(gross, rate);
	gross = calcGross(hours, rate);
	bonus = calcBonus(gross, rate);
	tax = calcTax(gross, tax);
	displayData(hours,rate,ot,gross, bonus, tax, insur);
	

	//display 
	system("pause");
	return 0;
}

double getHoursWorked()
{
	double hrs;
	cout << "Please enter the number of hours worked: ";
	cin >> hrs;
	return hrs;
}
double getPayRate()
{
	double pay;
	cout << "Please enter your pay rate: $";
	cin >> pay;
	return pay;
}

double calcTax(double grossPay, double taxAmt)
{

if(grossPay >100)
      taxAmt = grossPay * .15;
      else if(grossPay >500)
            taxAmt = grossPay * .10;
      else if(grossPay >1000)
                  taxAmt= grossPay * .05;
      else
                  taxAmt = 0;
return taxAmt;
}

double calcBonus(double grossPay, double bonusPay)
{
if(grossPay >100)
	bonusPay = 1000;
 else if(grossPay > 9000)
	bonusPay = 600;
 else
		 bonusPay = 0;
return bonusPay;
} 

double getInsRate(double insuranceType, double insRate)
{
	cout << "Insurance Type" ;
	cin >> insuranceType;

      if (insuranceType == '1')

            insRate = .02;

            else if (insuranceType == '2')

                  insRate = .03;

                  else if (insuranceType == '3')

                        insRate = .04;

                         else

                              insRate = .05;

return insRate;
}

double calcIns(double insRate, double grossPay, double insAmt)
{
      insAmt = 0;

      insAmt = insRate * grossPay;

return insAmt;
}
double calcGross(double &hrs, double pay)
{
	if(hrs >40)
	{
		ot = hrs - 40;
		hrs = 40;
	}
	return (hrs * pay) + (ot * pay * 1.5);
}
void displayData(double hours, double pay, double overTime, double grs, double bonusPay, double taxAmt, double getInsRate)
{	
	cout << fixed << setprecision(2);
	cout << "\nReg hours worked:" << setw(9)<< right << hours << endl;
	cout << "Bonus" << setw(9) << right << bonusPay << endl;
	cout << "Ins Rate" << setw(9) << right << calcIns << endl;
	cout << "Over Time worked:" << setw(9) << right << overTime << endl;
	cout << "Tax:" << setw(9) << right << taxAmt << endl;
	cout << "Rate of Pay:     " << setw(9) << right << pay << endl;
	cout << "Gross Pay:       " << setw(9) << right << grs << endl;
	cout << "\n\n";
}

It is supposed to multiply the insurance rate, depending on the insurance type the user inputs, times gross pay. All I get for Insurance rate is 00401440. No matter what I put when it asks for insurance type. While I realize this code might not look the best, because I'm just taking one section of a huge program at a time, I apologize and hope someone can help me figure this out. Thank you.

calcIns is a FUNCTION, not a VALUE in your program. You have no variable called calcIns. I assume you want to display the results of a CALL to the function, not the function itself. In that case, you need to call calcIns and assign a variable to equal the return value, then display that variable.

Thus line 124 should be changed to something like this.

// b, c, and d are variables of type double.  Replace them with whatever doubles you need to call that function.
double a = calcIns(b, c, d);
cout << "Ins Rate" << setw(9) << right << a << endl;

Edited 6 Years Ago by VernonDozier: n/a

calcIns is a FUNCTION, not a VALUE in your program. You have no variable called calcIns. I assume you want to display the results of a CALL to the function, not the function itself. In that case, you need to call calcIns and assign a variable to equal the return value, then display that variable.

Thus line 124 should be changed to something like this.

// b, c, and d are variables of type double.  Replace them with whatever doubles you need to call that function.
double a = calcIns(b, c, d);
cout << "Ins Rate" << setw(9) << right << a << endl;

I don't quite get what your saying. Why can't I do this:

cout << "Ins Rate" << setw(9) << right << insAmt << endl;

Because in my calcIns function it returns insAmt. But when I run the program and type "1" as the insurance type it gives me .05 for $164 gross pay. It should be somewhere where around $3.00. $164*.02

I found out when I run the program it is multiplying my insurance rate times the hours worked. I can't find out why because my ins function looks just like my tax function and the tax function displays the right value. I revised my code a little too.....

#include<iostream>
#include<iomanip>
using namespace std;

double ot = 0;
double getHoursWorked();
double getPayRate();
double calcTax(double grossPay, double taxAmt);
double grossPay();
double calcIns(double insRate, double grossPay, double insAmt);
double getInsRate(double insuranceType, double insRate, double grossPay);
double calcBonus(double grossPay, double bonusPay);
double calcGross(double, double, double);
void displayData(double, double, double, double, double, double, double );

int main()
{
	//declare variables
	double hours = 0.0, rate = 0.0, grossPay = 0.0, bonus = 0.0, tax = 0.0, insur = 0.0, insRate = 0.0;

	

	//get and calculate hours and pay
	hours = getHoursWorked();
	rate = getPayRate();
	insur = getInsRate(grossPay, rate, grossPay);
	grossPay = calcGross(hours, rate, tax);
	bonus = calcBonus(grossPay, rate);
	tax = calcTax(grossPay, tax);
	displayData(hours,rate,ot,grossPay, bonus, tax, insur);
	

	//display 
	system("pause");
	return 0;
}

double getHoursWorked()
{
	double hrs;
	cout << "Please enter the number of hours worked: ";
	cin >> hrs;
	return hrs;
}
double getPayRate()
{
	double pay;
	cout << "Please enter your pay rate: $";
	cin >> pay;
	return pay;
}
double calcGross(double hrs, double pay, double grossPay)
{
	grossPay = pay * hrs;
	return grossPay;
}
double calcTax(double grossPay, double taxAmt)
{

if(grossPay >100)
      taxAmt = grossPay * .15;
      else if(grossPay >500)
            taxAmt = grossPay * .10;
      else if(grossPay >1000)
                  taxAmt= grossPay * .05;
      else
                  taxAmt = 0;
return taxAmt;
}



double getInsRate(double insuranceType, double insRate, double grossPay)
{
	cout << "Insurance Type" ;
	cin >> insuranceType;

      if (insuranceType == 1)

            insRate = .02 * grossPay;

            else if (insuranceType == '2')

                  insRate = .03 * grossPay;

                  else if (insuranceType == '3')

                        insRate = .04 * grossPay;

                         else

                              insRate = .05 * grossPay;

return insRate;
}

double calcBonus(double grossPay, double bonusPay)
{
if(grossPay >100)
	bonusPay = 1000;
 else if(grossPay > 9000)
	bonusPay = 600;
 else
		 bonusPay = 0;
return bonusPay;
} 




void displayData(double hours, double pay, double overTime, double grossPay, double bonusPay, double taxAmt, double insRate)
{	
	cout << fixed << setprecision(2);
	cout << "\nReg hours worked:" << setw(9)<< right << hours << endl;
	cout << "Bonus" << setw(9) << right << bonusPay << endl;
	cout << "Ins Rate" << setw(9) << right << insRate << endl;
	cout << "Over Time worked:" << setw(9) << right << overTime << endl;
	cout << "Tax:" << setw(9) << right << taxAmt << endl;
	cout << "Rate of Pay:     " << setw(9) << right << pay << endl;
	cout << "Gross Pay:       " << setw(9) << right << grossPay << endl;
	cout << "\n\n";
}

I had a few edits, not noticing the code changes between posts 3 and 4 earlier.

double getInsRate(double insuranceType, double insRate, double grossPay)
{
	cout << "Insurance Type" ;
	cin >> insuranceType;

      if (insuranceType == 1)

            insRate = .02 * grossPay;

            else if (insuranceType == '2')

                  insRate = .03 * grossPay;

                  else if (insuranceType == '3')

                        insRate = .04 * grossPay;

                         else

                              insRate = .05 * grossPay;

return insRate;
}

In fact, this function should not take a parameter called insRate because it is immediately overwritten in the function. Therefore it is useless to pass it to the function. Same thing with insuranceType. You immediately overwrite it with a cin statement.

You should therefore change them to local variables within the function and change your function to this. Note my comments. Also change line 10 to match this.

double getInsRate(double grossPay) // removed two parameters
{
        double insRate;  // declared as local variable - Vern
        int insuranceType;  //declared as local variable and changed type to integer - Vern
	cout << "Insurance Type" ;
	cin >> insuranceType;

      if (insuranceType == 1)

            insRate = .02 * grossPay;

            else if (insuranceType == 2) // removed quotes - Vern

                  insRate = .03 * grossPay;

                  else if (insuranceType == 3) // removed quotes - Vern

                        insRate = .04 * grossPay;

                         else

                              insRate = .05 * grossPay;

return insRate;
}

Change line 26 to this

insur = getInsRate(grossPay);

See if that works better. It actually WON'T work because you call the function BEFORE you calculate grossPay. So put line 27 BEFORE line 26. The other changes

Edited 6 Years Ago by VernonDozier: n/a

I don't quite get what your saying. Why can't I do this:

cout << "Ins Rate" << setw(9) << right << insAmt << endl;

Because in my calcIns function it returns insAmt. But when I run the program and type "1" as the insurance type it gives me .05 for $164 gross pay. It should be somewhere where around $3.00. $164*.02

See my last post. I got a little confused since as of THIS post, calcIns still had not been called. Your UPDATED code called it, but called it too soon. Note my comments in the updated function that I changed in my last post.

I'm not sure how much of this post is obsolete given your updated code. Variable names can get a little tricky. What you name a variable that you declare INSIDE of a function only keeps its name INSIDE of that function. That's called "variable scoping". It can get a little tricky. Not sure if you still have the question about variable names and when they go out of scope or not. Post again if you can't get the code working after reading my last post.

Thank you so much! I'll probably be needing some more help with all of this before Friday. (If you don't mind?) I've just been trying to put part by part together because I just started learning about this stuff. Thanks again!

I just have one last question and then I might be able to figure this program out. I've updated my code trying to add another part of the project in. It runs fine but I can't get it to stop saying "Enter employee position". Once I type M as the employee position it just says "enter employee position over and over again. I understand how the call function works now. I just having a hard time knowing where to put the cin or cout statements, rather I put them in the main function of a function as it's own. I guess my question is how do I stop the while loop? This is my updated code:

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

double ot = 0;
double getHoursWorked();
double calcTax(double grossPay, double taxAmt);
double grossPay();
double employeePos;
double getPayRate(int employeePos, double payRate);
double calcIns(double insRate, double grossPay, double insAmt);
double getInsRate(double grossPay);
double calcBonus(double grossPay, double bonusPay);
double calcGross(double, double, double);
void displayData(double, double, double, double, double, double, double );

int main()
{
    //declare variables
    double hours = 0.0, rate = 0.0, pay = 0.0, grossPay = 0.0, bonus = 0.0, tax = 0.0, insur = 0.0, insRate = 0.0, position = 0.0;
	int employeePos = 0.0;
   

 // end while
while (employeePos != 'm' && employeePos != 's' && employeePos != 'e')

{ 
      cout << "Enter the employee postion.";
      cin >> employeePos;
} // end while

    //get and calculate hours and pay
	
	pay = getPayRate(hours, rate);
    hours = getHoursWorked();
    grossPay = calcGross(hours, rate, tax);
    insur = getInsRate(grossPay);
    bonus = calcBonus(grossPay, rate);
    tax = calcTax(grossPay, tax);
    displayData(hours,rate,ot,grossPay, bonus, tax, insur);
   

    //display
    system("pause");
    return 0;
}


double getPayRate(int employeePos, double payRate)
{
     if (employeePos == 'm')

            payRate = 700;

	 else if (employeePos == 's')

            payRate = 600;

                  else 
                        payRate = 500;

return payRate;
}
double getHoursWorked()
{
    double hrs;
    cout << "Please enter the number of hours worked: ";
    cin >> hrs;
    return hrs;
}

double calcGross(double hrs, double payRate, double grossPay)
{
    grossPay = payRate * hrs;
    return grossPay;
}
double calcTax(double grossPay, double taxAmt)
{

if(grossPay >100)
      taxAmt = grossPay * .15;
      else if(grossPay >500)
            taxAmt = grossPay * .10;
      else if(grossPay >1000)
                  taxAmt= grossPay * .05;
      else
                  taxAmt = 0;
return taxAmt;
}



double getInsRate(double grossPay) // removed two parameters
{
        double insRate;  // declared as local variable - Vern
        int insuranceType;  //declared as local variable and changed type to integer - Vern
    cout << "Insurance Type" ;
    cin >> insuranceType;

      if (insuranceType == 1)

            insRate = .02 * grossPay;

            else if (insuranceType == 2) // removed quotes - Vern

                  insRate = .03 * grossPay;

                  else if (insuranceType == 3) // removed quotes - Vern

                        insRate = .04 * grossPay;

                         else

                              insRate = .05 * grossPay;

return insRate;
}

double calcBonus(double grossPay, double bonusPay)
{
if(grossPay >100)
    bonusPay = 1000;
 else if(grossPay > 9000)
    bonusPay = 600;
 else
         bonusPay = 0;
return bonusPay;
}




void displayData(double hours, double overTime, double grossPay,double payRate, double bonusPay, double taxAmt, double insRate)
{   
    cout << fixed << setprecision(2);
    cout << "\nReg hours worked:" << setw(9)<< right << hours << endl;
    cout << "Bonus" << setw(9) << right << bonusPay << endl;
    cout << "Ins Rate" << setw(9) << right << insRate << endl;
    cout << "Over Time worked:" << setw(9) << right << overTime << endl;
    cout << "Tax:" << setw(9) << right << taxAmt << endl;
   
    cout << "Gross Pay:       " << setw(9) << right << payRate << endl;
    cout << "\n\n";
}

This is what it needs to do:

"This function should contain a loop that processes employee payroll data until a sentinel value is entered by the user. A typical case would be to condition the loop on the employee number, checking for a sentinel value in the employee number field. The main function should also contain a loop that validates the employee position entered by the user. For example, a while loop conditioned by multiple comparisons might be used (employee position != ‘m’ && employee position != ‘s’ && employee position != ‘e’). "

Edited 6 Years Ago by Justin9825: edited

"This function should contain a loop that processes employee payroll data until a sentinel value is entered by the user. A typical case would be to condition the loop on the employee number, checking for a sentinel value in the employee number field. The main function should also contain a loop that validates the employee position entered by the user. For example, a while loop conditioned by multiple comparisons might be used (employee position != ‘m’ && employee position != ‘s’ && employee position != ‘e’). "

Ummmmm.... I don't see where you are even processing an employee ID number. I don't think you're meeting the assignment criteria.

I suggest you figure this part out in a new file, then start adding in what you have come up with so far here.

Edited 6 Years Ago by Fbody: n/a

I know. Trust me I know this code probably looks like crap. I was trying to get the hard parts out of the way. I just can't figure out why the while loop isn't stopping. See the pay rate is based on the employee position. trying to figure out how to code the employee position in the criteria it states.

It's a type problem. You are mixing up doubles, ints, and chars.

If the variable should take an 'm' as a value, you should make its type char. If it only takes integers, make it an int. If you are going to compare it using the == operator, generally don't make it a double. If you are going to use decimal points, make it a double. You declare employeePos an int and assign it to equal 0.0 (decimal, don't use it with int). Then you try to assign it to equal a letter. You can't do that with an int. Make employeePos a char.

I figureded it was something simple. Can you help me unserstand what the teacher wants when she says:

This function should contain a loop that processes employee payroll data until a sentinel value is entered by the user. A typical case would be to condition the loop on the employee number, checking for a sentinel value in the employee number field.
The main function should also contain a loop that validates the employee position entered by the user.
For example, a while loop conditioned by multiple comparisons might be used (employee position != ‘m’ && employee position != ‘s’ && employee position != ‘e’).
A validation loop for employee hours entered by the user should also be used to only accept a value greater than zero.)
Employee insurance type entered by the user should be validated using a while loop with multiple conditions (employee insurance type != 1 && employee insurance type != 2 . . . ).


Did I do what she wanted for employee position? What exactly is it doing?

He means

const int sentinelValue = -1; //define a sentinel
int inputValue = 0;  //declare an input variable

cin >> inputValue;  //get the initial input

while(inputValue != sentinelValue) {
  /* ... do something with inputValue ... */
  cin >> inputValue; //get a new inputValue
} //wend

This is just one version, there are other versions as well. The specific version you use depends on what your ending condition is.

Edited 6 Years Ago by Fbody: n/a

I hope you guys don't give up on me and think I'm just dumb. But I don't understand what I've done now. Now it won't calculate anything. It runs the prompts fine. Just the output displays nothing at all. I guess because I can't get the gross to calculate right which is why the rest isn't working because it depends on that gross pay.

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;

double ot = 0;

double calcTax(double grossPay, double taxAmt);
double grossPay();
double payRate();
double getPayRate(char employeePos, double payRate);
double calcIns(double insRate, double grossPay, double insAmt);
double getInsRate(double grossPay);
double calcBonus(double grossPay, double bonusPay);
double calcGrossPay(double, double);
void displayData(double, double, double, double, double, double );

int main()
{
    //declare variables
    double hours = 0.0, rate = 0.0, pay = 0.0, payRate = 0.0, gross = 0.0, employeeHrs = 0.0, bonus = 0.0, tax = 0.0, insur = 0.0, insRate = 0.0, position = 0.0;
	char employeePos = 0.0;
	char employeeNum = 0;
   
 while(employeeNum != '0')
{
            cout << "Enter the employee number. Enter a 0 (zero) to quit:  ";
            cin >> employeeNum;            
}
 // end while


while (employeePos != 'm' && employeePos != 's' && employeePos != 'e')

{ 
      cout << "Enter the employee postion.";
      cin >> employeePos;
} // end while

while (employeeHrs <= 0)

{
      cout << "Enter the hours worked. It must be greater than 0: ";
      cin >> employeeHrs;
      if (employeeHrs <= 0)
            cout << "Your hours worked must be > 0. Please enter a valid value";
}
    //get and calculate hours and pay
	
	double getPayRate(char employeePos, double payRate);
   
    double calcGrossPay(double payRate, double employeeHrs);
    double getInsRate(double gross);
    double calcBonus(double gross, double bonusPay);
    double calcTax(double gross, double taxAmt);
    displayData(rate,ot,gross, bonus, tax, insur);
   

    //display
    system("pause");
    return 0;
}


double getPayRate(char employeePos, double payRate)
{
     if (employeePos == 'm')

            payRate = 700;

	 else if (employeePos == 's')

            payRate = 600;

                  else 
                        payRate = 500;

return payRate;
}


double calcGrossPay(double payRate, double employeeHrs)
{
double grossPay = 0;
grossPay = payRate * employeeHrs;
return grossPay;
}

double calcTax(double gross, double taxAmt)
{

if(gross >100)
      taxAmt = gross * .15;
      else if(gross >500)
            taxAmt = gross * .10;
      else if(gross >1000)
                  taxAmt= gross * .05;
      else
                  taxAmt = 0;
return taxAmt;
}



double getInsRate(double gross) // removed two parameters
{
        double insRate;  // declared as local variable - Vern
        int insuranceType;  //declared as local variable and changed type to integer - Vern
    cout << "Insurance Type" ;
    cin >> insuranceType;

      if (insuranceType == 1)

            insRate = .02 * gross;

            else if (insuranceType == 2) // removed quotes - Vern

                  insRate = .03 * gross;

                  else if (insuranceType == 3) // removed quotes - Vern

                        insRate = .04 * gross;

                         else

                              insRate = .05 * gross;

return insRate;
}

double calcBonus(double gross, double bonusPay)
{
if(gross >100)
    bonusPay = 1000;
 else if(gross > 9000)
    bonusPay = 600;
 else
         bonusPay = 0;
return bonusPay;
}




void displayData(double overTime, double grossPay,double payRate, double bonusPay, double taxAmt, double insRate)
{   
    cout << fixed << setprecision(2);
   
    cout << "Bonus" << setw(9) << right << bonusPay << endl;
    cout << "Ins Rate" << setw(9) << right << insRate << endl;
    cout << "Over Time worked:" << setw(9) << right << overTime << endl;
    cout << "Tax:" << setw(9) << right << taxAmt << endl;
   
    cout << "Gross Pay:       " << setw(9) << right << grossPay << endl;
    cout << "\n\n";
}

I hope you guys don't give up on me and think I'm just dumb. But I don't understand what I've done now. Now it won't calculate anything. It runs the prompts fine. Just the output displays nothing at all. I guess because I can't get the gross to calculate right which is why the rest isn't working because it depends on that gross pay.

Double and integers will always output something. If there is no output, then the cout statement is either never called or the program exits early, either by either crashing or exiting normally. You should stick some grafitti in your program and see how far the program gets. Sprinkle stuff like this:

cout << "Vern was here 1\n";
cout << "Vern was here 2\n";
cout << "Vern was here 3\n";
cout << "Vern was here 4\n";

I stick the different numbers in there so each printout is different and I can see exactly where the program has been. Stick a few in there and see which print and which don't. Take them out later. It'll take you a few tries, but hopefully very soon you can find exactly what the program is doing that it shouldn't. Take the statements out later.

Or you can use a debugger and stick breakpoints in if you know how to use a debugger.

Comments
You and I went to the same school of debugging lol.
#include<iostream>
#include<iomanip>
#include<string>
using namespace std;



double calcTax(double grossPay, double taxAmt);
double grossPay();
double payRate();
double getPayRate(char employeePos, double payRate);
double calcIns(double insRate, double grossPay, double insAmt);
double getInsRate(double grossPay);
double calcBonus(double grossPay, double bonusPay);
double calcGrossPay(double, double);
void displayData(double, double, double, double, double, double );

int main()
{
    //declare variables
    double hours = 0.0, rate = 0.0, pay = 0.0, payRate = 0.0, gross = 0.0,bonus = 0.0, tax = 0.0, insur = 0.0, insRate = 0.0, position = 0.0;
	int employeeHrs = 0;
	char employeePos = 0;
   
 // end while



while (employeePos != 'm' && employeePos != 's' && employeePos != 'e')
{ 
      cout << "Enter the employee postion.";
      cin >> employeePos;
} // end while

while (employeeHrs <= 0)

{
	cout << "Enter the hours worked: ";
      cin >> employeeHrs;
      
}
    //get and calculate hours and pay
	
	rate = getPayRate(employeePos, payRate);
 
    gross = calcGrossPay(payRate, employeeHrs);
	
    double getInsRate(double gross);
    double calcBonus(double gross, double bonusPay);
    double calcTax(double gross, double taxAmt);
    displayData(rate, position, gross, bonus, tax, insur);
   

    //display
    system("pause");
    return 0;
}


double getPayRate(char employeePos, double payRate)
{
     if (employeePos == 'm')

            payRate = 700;

	 else if (employeePos == 's')

            payRate = 600;

                  else 
                        payRate = 500;

return payRate;
}


double calcGrossPay(double payRate, double employeeHrs)
{
double grossPay = 0;
grossPay = payRate * employeeHrs;
return grossPay;
}

double calcTax(double gross, double taxAmt)
{

if(gross >100)
      taxAmt = gross * .15;
      else if(gross >500)
            taxAmt = gross * .10;
      else if(gross >1000)
                  taxAmt= gross * .05;
      else
                  taxAmt = 0;
return taxAmt;
}



double getInsRate(double gross) // removed two parameters
{
        double insRate;  // declared as local variable - Vern
        int insuranceType;  //declared as local variable and changed type to integer - Vern
    cout << "Insurance Type" ;
    cin >> insuranceType;

      if (insuranceType == 1)

            insRate = .02 * gross;

            else if (insuranceType == 2) // removed quotes - Vern

                  insRate = .03 * gross;

                  else if (insuranceType == 3) // removed quotes - Vern

                        insRate = .04 * gross;

                         else

                              insRate = .05 * gross;

return insRate;
}

double calcBonus(double gross, double bonusPay)
{
if(gross >100)
    bonusPay = 1000;
 else if(gross > 9000)
    bonusPay = 600;
 else
         bonusPay = 0;
return bonusPay;
}




void displayData(double grossPay, double payRate, double bonusPay, double taxAmt, double insRate, double positin)
{   
    cout << fixed << setprecision(2);
   
    cout << "Bonus" << setw(9) << right << bonusPay << endl;
    cout << "Ins Rate" << setw(9) << right << insRate << endl;

    cout << "Tax:" << setw(9) << right << taxAmt << endl;
   
    cout << "Gross Pay:       " << setw(9) << right << grossPay << endl;
    cout << "\n\n";
}

Now it seems to be working. I don't know what I did though. But instead of multiplying the hours by 700 for manager "m". It doesn't do that. It displays the gross pay as 700 it never multiples it by the hours worked. Am I not calling the function after getPayRate?

Employee position is returning 109'm' whenever I type m?

'm' is 109 in the ASCII table. 10 bucks says you were assigning an integer to be the value 'm'. You need to be careful about your types and make them consistent.

Now it seems to be working. I don't know what I did though. But instead of multiplying the hours by 700 for manager "m". It doesn't do that. It displays the gross pay as 700 it never multiples it by the hours worked. Am I not calling the function after getPayRate?

This is another example of you not using global scoping correctly. Or perhaps not using pass-by-reference correctly. Or not assigning the return value to the proper value. Or having unnecessary parameters in the function. There is more than one way to do this correctly if you change one or more of the four items I listed. Your particular combination won't work though.

rate = getPayRate(employeePos, payRate); 
gross = calcGrossPay(payRate, employeeHrs);

double getPayRate(char employeePos, double payRate)
{
     if (employeePos == 'm')

            payRate = 700;

	 else if (employeePos == 's')

            payRate = 600;

                  else 
                        payRate = 500;

return payRate;
}

The changes you make to the green variable will have no effect on the red variable. The green payRate is a local variable in your function. Any changes to it don't make it back to the red payRate in main.

payRate is a completely useless parameter for this function since you do nothing with the value passed to it. Therefore it needs to not be a parameter. It needs to be a local variable in the function. Change the function to this:

rate = getPayRate(employeePos);
gross = calcGrossPay(payRate, employeeHrs);

double getPayRate(char employeePos)
{
     double payRate;
     if (employeePos == 'm')

            payRate = 700;

	 else if (employeePos == 's')

            payRate = 600;

                  else 
                        payRate = 500;

return payRate;
}

Now to the meat of the matter:

rate = getPayRate(employeePos);
gross = calcGrossPay(payRate, employeeHrs);

You assign the return value of the function to equal rate, not payRate, then you call the next function using payRate, which hasn't changed with that last function call. Change it all to this:

payRate = getPayRate(employeePos);
gross = calcGrossPay(payRate, employeeHrs);

double getPayRate(char employeePos)
{
     double payRate;
     if (employeePos == 'm')

            payRate = 700;

	 else if (employeePos == 's')

            payRate = 600;

                  else 
                        payRate = 500;

return payRate;
}

Note the red and the green. Everything matches now.

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;



double calcTax(double grossPay, double taxAmt);
double grossPay();
double payRate();
double getPayRate(char employeePos);
double calcIns(double insRate, double grossPay, double insAmt);
double getInsRate(double grossPay);
double calcBonus(double grossPay);
double calcGrossPay(double, double);
void displayData(double, double, double, double, double, double );

int main()
{
    //declare variables
    double hours = 0.0, rate = 0.0, pay = 0.0, payRate = 0.0, gross = 0.0,bonus = 0.0, tax = 0.0, insur = 0.0, insRate = 0.0, position = 0.0;
	int employeeHrs = 0;
	char employeePos = 0;
   
 // end while



while (employeePos != 'm' && employeePos != 's' && employeePos != 'e')
{ 
      cout << "Enter the employee postion.";
      cin >> employeePos;
} // end while

while (employeeHrs <= 0)

{
	cout << "Enter the hours worked: ";
      cin >> employeeHrs;
      
}
    //get and calculate hours and pay
	
	payRate = getPayRate(employeePos);
	gross = calcGrossPay(payRate, employeeHrs);
	
    double getInsRate(double gross);
    double calcBonus(double gross);
    double calcTax(double gross);
    displayData(rate, position, gross, bonus, tax, insur);
   

    //display
    system("pause");
    return 0;
}





double getPayRate(char employeePos)
{
     double payRate;
     if (employeePos == 'm')

            payRate = 700;

	 else if (employeePos == 's')

            payRate = 600;

                  else 
                        payRate = 500;

return payRate;
}


double calcGrossPay(double payRate, double employeeHrs)
{
double grossPay = 0;
grossPay = payRate * employeeHrs;
return grossPay;
}

double calcTax(double gross)
{
double taxAmt;
if(gross >100)
      taxAmt = gross * .15;
      else if(gross >500)
            taxAmt = gross * .10;
      else if(gross >1000)
                  taxAmt= gross * .05;
      else
                  taxAmt = 0;
return taxAmt;
}



double getInsRate(double gross) // removed two parameters
{
        double insRate;  // declared as local variable - Vern
        int insuranceType;  //declared as local variable and changed type to integer - Vern
    cout << "Insurance Type" ;
    cin >> insuranceType;

      if (insuranceType == 1)

            insRate = .02 * gross;

            else if (insuranceType == 2) // removed quotes - Vern

                  insRate = .03 * gross;

                  else if (insuranceType == 3) // removed quotes - Vern

                        insRate = .04 * gross;

                         else

                              insRate = .05 * gross;

return insRate;
}

double calcBonus(double gross)
{
	double bonusPay;
if(gross >100)
    bonusPay = 1000;
 else if(gross > 9000)
    bonusPay = 600;
 else
         bonusPay = 0;
return bonusPay;
}




void displayData(double grossPay, double payRate, double bonusPay, double taxAmt, double insRate, double positin)
{   
    cout << fixed << setprecision(2);
   
    cout << "Bonus" << setw(9) << right << bonusPay << endl;
    cout << "Ins Rate" << setw(9) << right << insRate << endl;

    cout << "Tax:" << setw(9) << right << taxAmt << endl;
   
    cout << "Gross Pay:       " << setw(9) << right << grossPay << endl;
    cout << "\n\n";
}

Ok I changed what you said and I think I changed all my other functions the way they should be? But now it shows the correct gross pay it just shows it in my bonus pay output?

Look at Lines 50 and 144. The arrangement of your arguments does not match the arrangement of your parameters.

There! I finally got everything I have working. Now to clean it up and add the rest of the assignment code to it.... Hopefully I won't need anymore help. Thanks a lot you guys!

Ok,, I've done a lot of work to this program. My only problem now, which seems to be a simple one, is that I can't get the employee position to work. When I make it int it just doesn't work at all, it just asks for position instead of number. When I make it a char, it works. But it jumbles up all the text when it asks for position.

//Justin Toler
//Final Project CIS 115 900
//4-30-2010
//Extra Credit: Added 401K function to caculate amount to take out for 401K plan

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
//declare function variables
double calcTax(double);
double grossPay();
double payRate();
double calcNetPay(double, double);
double calcTotalDed(double, double, double, double, double);
double getPayRate(char);
double calcIns(double, double, double);
double getInsRate(double, int);
double calcRetire(double);
double calcBonus(double);
double calcTotalPay(double, double);
double calcGrossPay(double, double);
void displayOutputData(double, double, double, double, double, double, double, double, double, int const);
void displayInputData(double, char, int);

int main()
{
    //declare variables
    double hours = 0.0, rate = 0.0, pay = 0.0, payRate = 0.0, gross = 0.0,
		bonus = 0.0, tax = 0.0, insurance = 0.0, 
		insRate = 0.0, position = 0.0,ssAmt = 0.0, 
		ded = 0.0, totalPay = 0.0, netPay = 0.0, retire = 0.0;
	int employeeHrs = 0, insuranceType = 0; 
	int employeeNum = 0;
	char employeePos = 0 ;
	int const medicalAmt = 100;

 while(employeeNum != 0)
{
    cout << "Enter the employee number. " ;
        cin >> employeeNum;
         
}
while (employeePos != 'm' && employeePos != 's' && employeePos != 'e')
{ 
	cout << "Enter the employee postion: ";
      cin >> employeePos;
}
// end while

while (employeeHrs <= 0)
{
	cout << "Enter the hours worked: ";
      cin >> employeeHrs;
} //end while
while(insuranceType!= 1 && insuranceType != 2 && insuranceType != 3 && insuranceType != 4)
{
	cout << "Enter your insurance type.\nEnter 1 for single, \n2 for single with children, \n3 for married, or \n4 for married with children:";
	  cin >> insuranceType;
} 
//end while
    //get and calculate hours and pay	
	payRate = getPayRate(employeePos);
	gross = calcGrossPay(payRate, employeeHrs);
	bonus = calcBonus(gross);
	totalPay = calcTotalPay(gross, bonus);
    insurance = getInsRate(gross, insuranceType);    
    tax = calcTax(gross);
	ssAmt = gross * .05;
	retire = calcRetire(totalPay);
	ded = calcTotalDed(insurance, tax, ssAmt, medicalAmt, retire);
	netPay = calcNetPay(totalPay, ded);
	system("cls");
	displayInputData(employeeHrs, employeePos, insuranceType);
    displayOutputData(gross, bonus, tax, insurance, ssAmt, ded, totalPay, netPay, retire, medicalAmt);
	

	   

	    
    system("pause");
    return 0;
}

double getPayRate(char employeePos)// Deteremine employee pay per hour
{
     double payRate;
     if (employeePos == 'm')
            payRate = 700;
	 else if (employeePos == 's')
            payRate = 600;
                  else 
                    payRate = 500;
return payRate;
}

double calcGrossPay(double payRate, double employeeHrs) // Calculate Gross Pay
{
double grossPay = 0;
grossPay = payRate * employeeHrs;
return grossPay;
}

double calcTax(double gross) // Determine tax amount based on gross pay
{
double taxAmt;
if(gross >100)
      taxAmt = gross * .15;
      else if(gross >500)
            taxAmt = gross * .10;
      else if(gross >1000)
                  taxAmt= gross * .05;
      else
                  taxAmt = 0;
return taxAmt;
}

double getInsRate(double gross, int insuranceType) //Get insurance rate
{
        double insRate; 
        		
      if (insuranceType == 1)
            insRate = .02 * gross;
            else if (insuranceType == 2)
                  insRate = .03 * gross;
               else if (insuranceType == 3)
                        insRate = .04 * gross;
                   else
                        insRate = .05 * gross;
return insRate;
}

double calcBonus(double gross) //Calculate bonus based on gross pay
{
	double bonusPay;

if(gross >= 12000)
    bonusPay = 1000;
 else if(gross >= 9000)
    bonusPay = 600;
 else
     bonusPay = 0;
return bonusPay;
}

double calcRetire(double totalPay) //Caculate amount for 401K
{
	double retireCost;
	retireCost = totalPay * .02;
return retireCost;
}

double calcTotalPay(double grossPay, double bonusPay) //Caculate total pay before deductions
{
	double totalPay;
	totalPay = grossPay + bonusPay;

return totalPay;
}

double calcTotalDed(double insRate, double taxAmt, double ssAmt, double medicalAmt, double retireCost) //Caculate Total Deductions
{
	double totalDeductions;
	totalDeductions = insRate + taxAmt + ssAmt + medicalAmt + retireCost;	

return totalDeductions;
}

double calcNetPay(double totalPay, double totalDeductions) //Caculate total net pay after deductions (take home pay)
{
	double totalNetPay;
	totalNetPay = totalPay - totalDeductions;

return totalNetPay;
}


void displayInputData(double employeeHrs, char employeePos, int insuranceType)
{
	cout << fixed << setprecision(2);
	cout << "               Employee Input Data               " << endl;
	cout << "\n\n";
	cout << "Employee Position:          " << setw(9) << right << employeePos << endl;
	cout << "Employee Hours Worked:      " << setw(9) << right << employeeHrs << endl;
	cout << "Employee Insurance Type:    " << setw(9) << right << insuranceType << endl;
	
	
}
void displayOutputData(double grossPay, double bonusPay, double taxAmt, double insRate, double ssAmt, double totalDeductions, 
				 double totalPay, double totalNetPay, double retireCost, int const medicalCost)
{   
	cout << "****************************************" << endl;
    cout << fixed << setprecision(2);  
	cout << "               Employee Payroll Data               " << endl;
	cout << "\n\n";
	cout << "Employee Gross Pay:           " << setw(9) << right << "$" << grossPay << endl;
	cout << "Employee Bonus Pay:           " << setw(9) << right << "$" << bonusPay << endl;
	cout << "Employee Total Pay:           " << setw(14) << right << "$" << totalPay << endl;
	cout << "\n\n";
	cout << "Employee Insurance Rate:      " << setw(9) << right << "$" << insRate << endl;
	cout << "Employee Tax Amount:          " << setw(9) << right << "$" << taxAmt << endl; 
	cout << "Employee S. S. Amount:           " << setw(9) << right << "$" << ssAmt << endl;
    cout << "Employee Medical Cost:        " << setw(9) << right << "$" << medicalCost << endl;
	cout << "Employee 401K Amount:         " << setw(9) << right << "$" << retireCost << endl;
	cout << "Employee Total Deductions:    " << setw(14) << right << "$" << totalDeductions << endl;    
	cout << "Employee Net Pay:             " << setw(14) << right << "$" << totalNetPay << endl;
    cout << "\n\n";
}

Doesn't look jumbled to me. Please elaborate on the problem. Here's a run:

Enter the employee postion: m
Enter the hours worked: 40
Enter your insurance type.
Enter 1 for single, 
2 for single with children, 
3 for married, or 
4 for married with children:

1

Employee Input Data               


Employee Position:                  m
Employee Hours Worked:          40.00
Employee Insurance Type:            1
****************************************
               Employee Payroll Data               


Employee Gross Pay:                   $28000.00
Employee Bonus Pay:                   $1000.00
Employee Total Pay:                        $29000.00


Employee Insurance Rate:              $560.00
Employee Tax Amount:                  $4200.00
Employee S. S. Amount:                   $1400.00
Employee Medical Cost:                $100
Employee 401K Amount:                 $580.00
Employee Total Deductions:                 $6840.00
Employee Net Pay:                          $22160.00

See the previous code never asked for the employee number. But I fixed that problem. Now I'm just trying to figure out how to do what I did with employee number and hours to employee position and insurance code. I need to make so if you enter a invalid character. It re-asks the question. Also I was wondering how in the first question (Employee Number) how do I make it so the user can enter "0" to quit the program?

//Justin Toler
//Final Project CIS 115 900
//4-30-2010
//Extra Credit: Added 401K function to caculate amount to take out for 401K plan

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
//declare function variables
double calcTax(double);
double grossPay();
double payRate();
double calcNetPay(double, double);
double calcTotalDed(double, double, double, double, double);
double getPayRate(char);
double calcIns(double, double, double);
double getInsRate(double, int);
double calcRetire(double);
double calcBonus(double);
double calcTotalPay(double, double);
double calcGrossPay(double, double);
void displayOutputData(double, double, double, double, double, double, double, double, double, int const);
void displayInputData(double, double, char, int);

int main()
{
    //declare variables
    double hours = 0.0, rate = 0.0, pay = 0.0, payRate = 0.0, 
		gross = 0.0, bonus = 0.0, tax = 0.0, insurance = 0.0, 
		insRate = 0.0, position = 0.0,ssAmt = 0.0,ded = 0.0,
		totalPay = 0.0, netPay = 0.0, retire = 0.0, code = 0.0;
	int employeeHrs = 0, insuranceType = 0; 
	char employeePos = 0 ;
	int const medicalAmt = 100;

cout << "Please enter an employee number (between 0 and 2000): ";
    cin >> code;
while (code < 1 ||code > 2000)
    {
    cout<<"Invalid employee number!"<<endl;
    system("pause");
    system("cls");
	cout << "Please enter an employee number (between 0 and 2000): ";
    cin >> code;
    }
while (employeePos != 'm' && employeePos != 's' && employeePos != 'e')
{ 
	cout << "Enter the employee postion: ";
      cin >> employeePos;
}
// end while

cout << "Please enter the number of hours the employee worked: ";
    cin >> employeeHrs;
while(employeeHrs < 1)
{
  cout << "The employee must have worked a minimum of 1 hour to process payroll: ";
   cin >> employeeHrs;
}
 //end while
while(insuranceType!= 1 && insuranceType != 2 && insuranceType != 3 && insuranceType != 4)
{
	cout << "Enter your insurance type.\n1 for single, \n2 for single with children, \n3 for married, or \n4 for married with children: ";
	  cin >> insuranceType;
} 
//end while
    //get and calculate hours and pay	
	payRate = getPayRate(employeePos);
	gross = calcGrossPay(payRate, employeeHrs);
	bonus = calcBonus(gross);
	totalPay = calcTotalPay(gross, bonus);
    insurance = getInsRate(gross, insuranceType);    
    tax = calcTax(gross);
	ssAmt = gross * .05;
	retire = calcRetire(totalPay);
	ded = calcTotalDed(insurance, tax, ssAmt, medicalAmt, retire);
	netPay = calcNetPay(totalPay, ded);
	system("cls");
	displayInputData(code, employeeHrs, employeePos, insuranceType);
    displayOutputData(gross, bonus, tax, insurance, ssAmt, ded, totalPay, netPay, retire, medicalAmt);
	

	   

	    
    system("pause");
    return 0;
}

double getPayRate(char employeePos)// Deteremine employee pay per hour
{
     double payRate;
     if (employeePos == 'm')
            payRate = 700;
	 else if (employeePos == 's')
            payRate = 600;
                  else 
                    payRate = 500;
return payRate;
}

double calcGrossPay(double payRate, double employeeHrs) // Calculate Gross Pay
{
double grossPay = 0;
grossPay = payRate * employeeHrs;
return grossPay;
}

double calcTax(double gross) // Determine tax amount based on gross pay
{
double taxAmt;
if(gross >100)
      taxAmt = gross * .15;
      else if(gross >500)
            taxAmt = gross * .10;
      else if(gross >1000)
                  taxAmt= gross * .05;
      else
                  taxAmt = 0;
return taxAmt;
}

double getInsRate(double gross, int insuranceType) //Get insurance rate
{
        double insRate; 
        		
      if (insuranceType == 1)
            insRate = .02 * gross;
            else if (insuranceType == 2)
                  insRate = .03 * gross;
               else if (insuranceType == 3)
                        insRate = .04 * gross;
                   else
                        insRate = .05 * gross;
return insRate;
}

double calcBonus(double gross) //Calculate bonus based on gross pay
{
	double bonusPay;

if(gross >= 12000)
    bonusPay = 1000;
 else if(gross >= 9000)
    bonusPay = 600;
 else
     bonusPay = 0;
return bonusPay;
}

double calcRetire(double totalPay) //Caculate amount for 401K
{
	double retireCost;
	retireCost = totalPay * .02;
return retireCost;
}

double calcTotalPay(double grossPay, double bonusPay) //Caculate total pay before deductions
{
	double totalPay;
	totalPay = grossPay + bonusPay;

return totalPay;
}

double calcTotalDed(double insRate, double taxAmt, double ssAmt, double medicalAmt, double retireCost) //Caculate Total Deductions
{
	double totalDeductions;
	totalDeductions = insRate + taxAmt + ssAmt + medicalAmt + retireCost;	

return totalDeductions;
}

double calcNetPay(double totalPay, double totalDeductions) //Caculate total net pay after deductions (take home pay)
{
	double totalNetPay;
	totalNetPay = totalPay - totalDeductions;

return totalNetPay;
}


void displayInputData(double code, double employeeHrs, char employeePos, int insuranceType)
{
	
	cout << "               Employee Input Data               " << endl;
	cout << "\n\n";
	cout << "Employee Number:            " << setw(9) << right << code << endl;
	cout << "Employee Position:          " << setw(9) << right << employeePos << endl;
	cout << "Employee Hours Worked:      " << setw(9) << right << employeeHrs << endl;
	cout << "Employee Insurance Type:    " << setw(9) << right << insuranceType << endl;
	
	
}
void displayOutputData(double grossPay, double bonusPay, double taxAmt, double insRate, double ssAmt, double totalDeductions, 
				 double totalPay, double totalNetPay, double retireCost, int const medicalCost)
{   
	cout << "**************************************************" << endl;
    cout << fixed << setprecision(2);  
	cout << "               Employee Payroll Data               " << endl;
	cout << "\n\n";
	cout << "Employee Gross Pay:           " << setw(9) << right << "$" << grossPay << endl;
	cout << "Employee Bonus Pay:           " << setw(9) << right << "$" << bonusPay << endl;
	cout << "Employee Total Pay:           " << setw(14) << right << "$" << totalPay << endl;
	cout << "\n\n";
	cout << "Employee Insurance Rate:      " << setw(9) << right << "$" << insRate << endl;
	cout << "Employee Tax Amount:          " << setw(9) << right << "$" << taxAmt << endl; 
	cout << "Employee S. S. Amount:           " << setw(9) << right << "$" << ssAmt << endl;
    cout << "Employee Medical Cost:        " << setw(9) << right << "$" << medicalCost << endl;
	cout << "Employee 401K Amount:         " << setw(9) << right << "$" << retireCost << endl;
	cout << "Employee Total Deductions:    " << setw(14) << right << "$" << totalDeductions << endl;    
	cout << "Employee Net Pay:             " << setw(14) << right << "$" << totalNetPay << endl;
    cout << "\n\n";
}

Ok never mind I got the first part of my problem. Just can you explain to me how I do this.... Please enter an employee number (between 0 and 2000) or enter 0 to quit? Making it so the user can quit the program?

Here's what your overall flow needs to be in the main function:

// Ask for employeeNum and read it from the keyboard.
while (employeeNum != 0)
{
     // most of your main function will go here
    // Ask for employeeNum and read it from the keyboard again.
}

// exit program

You "prime" the while function once by asking for the employee number BEFORE the whole loop, then you ask the same thing again at the bottom of the while loop. That's the sentinel value (0) part.

For valid data, do something like this:

bool goodData = false;
char employeePos;

while (!goodData)
{
    cout << "Enter employeePos : ";
    cin >> employeePos;

    if (/* test employeePos for valid data */
    {
        goodData = true;
    }
    else
    {
        // display error message.
    }
}

// data is good and stored in employeePos.  Now do something with it.

I did something like that, I think.

cout << "Enter the employee postion: ";
        cin >> employeePos;
while (employeePos != 'm' && employeePos != 's' && employeePos != 'e')
{
	 cout << "Invalid Code!\n\n" ;
	 cout << "Enter the employee postion: ";
        cin >> employeePos;
}

It works and repeats the question until it get m, e, or s. Any suggestions on the "0" to quit question? Btw.. Thanks so much for your help this is the last question then the program is complete!

It works and repeats the question until it get m, e, or s. Any suggestions on the "0" to quit question? Btw.. Thanks so much for your help this is the last question then the program is complete!

You're welcome. I mentioned the "quit" part in my last post.

// Ask for employeeNum and read it from the keyboard.
while (employeeNum != 0)
{
     // most of your main function will go here
    // Ask for employeeNum and read it from the keyboard again.
}

// exit program

This is the outermost while loop. Most of the rest of your main goes in here. When the user enters 0 for employeeNum at the very bottom, the while loop exits and the program exits since nothing will be below the while loop.

Your while condition is actually fine, but the loop ends way to soon. Move the bracket on line 46 to line 86. Copy lines 37 and 38 to right before line 86. You'll want them at those two places (right befor the while loop and at the very bottom of the while loop.

Oh I didn't see you mention that. Thanks so much for help. This was so stressful.

This article has been dead for over six months. Start a new discussion instead.