need help with current code, I am a newbie. keep in mind, that everything that can be, needs to be passed by value. and only three functions can be called from main. they are getEmployeeData, displayEmployeeInfo, and calculateGrossPay. and all functiontions must keep the header type below. Help is greatly appreciated.

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

bool getEmployeeData (int, char, char, int&, int&, int&, int);
float calculateGrossPay (int, char, int&, int&, int&, int);
void displayEmployeeInfo(int, char, int);
bool tryAgain(char, char);
bool isValidStatus(char, char);

int main()
{
        int ID, Salary, hourwrk, sales, grossPay;
        char pyrllSt = ' ';
        char quit;
        bool result = true;
        while (result)
        { 
                result = getEmployeeData (ID, pyrllSt, quit, Salary, hourwrk, sales, grossPay);

                if (result)
                {
                        displayEmployeeInfo (ID, pyrllSt, grossPay);
                }

                else
                {
                        calculateGrossPay(ID, pyrllSt, Salary, hourwrk, sales, grossPay);
                }
        }
        return (1);
}

bool getEmployeeData (int ID, char pyrllSt, char quit, int Salary, int hourwrk, int sales, int grossPay)
{
        cout << "Enter employee ID: " << endl;
        cin >> ID;

        if (ID > 0)
        {
                cout << "Enter payroll status: " << endl;
                cin >> pyrllSt;
                if (isValidStatus(pyrllSt, quit))
                {
                        if (pyrllSt == 's' || pyrllSt == 'S')
                        {
                                 cout << "Enter monthly salary: " << endl;
                        }

                        if (pyrllSt == 'h' || pyrllSt == 'H')
                        {
                                 cout << "Enter number of hours worked this month: " << endl;
                        }

                        if (pyrllSt == 'c' || pyrllSt == 'C')
                        {
                                 cout << "Enter total sales for this month: " << endl;
                        }
                }//if isValid Status
                return true;
        }
        else
        {        
                return false;
        }
}

bool isValidStatus (char pyrllSt, char quit)
{
        bool isGood = true;
        cout << "Enter payroll status: " << endl;
        cin >> pyrllSt;

        while (isGood)
        {
                isGood = (pyrllSt == 's' || pyrllSt == 'S' || pyrllSt == 'h' || pyrllSt == 'H' || pyrllSt == 'c' || pyrllSt =='C');
                if (isGood)
                {
                        return true;
                }
                else
                {
                        return tryAgain(pyrllSt, quit);
                }
        }

}

bool tryAgain(char pyrllSt, char quit)
{
        bool (toQuit) = true;
        cout << "Type Q or q to quit, any other key to continue: " << endl;
        cin >> quit;

        while (toQuit)
        {
                toQuit = (quit == 'q' || quit == 'Q');
                if (toQuit)
                {
                cout << "*** Program Terminated ***" << endl;
                        return false;
                }
        }

        while(!toQuit)
        {
                cout << "Re-enter: 's' or 'S' for salaried, 'h' or 'H' for hourly, " << endl;
                cout << "'c' or 'C' for commissioned." << endl;
                cin >> pyrllSt;
                if (pyrllSt == 's' || pyrllSt == 'S' || pyrllSt == 'h' || pyrllSt == 'H' || pyrllSt == 'c' || pyrllSt =='C')
                {
                        return true;
                }

}

float calculateGrossPay (int ID, char pyrllSt, int Salary, int hourwrk, int sales, int grossPay)
{
        if (pyrllSt == 's' || pyrllSt == 'S')
        {
                grossPay = Salary;
                displayEmployeeInfo(ID, pyrllSt, grossPay);
                return grossPay;
        }

        else if (pyrllSt == 'h' || pyrllSt == 'H')
        {
                grossPay = hourwrk * 18.75;
                displayEmployeeInfo(ID, pyrllSt, grossPay);
                return grossPay;
        }

        else if (pyrllSt == 'c' || pyrllSt == 'C')
        {
                grossPay = sales * .06 + 1000.00;
                displayEmployeeInfo(ID, pyrllSt, grossPay);
                return grossPay;
        }
}

void displayEmployeeInfo (int ID, char pyrllSt, int grossPay)
{
    cout << "Employee ID: " << ID << endl;
    cout << "Payroll Status: " << pyrllSt << endl;
    cout << "Gross Pay: " << grossPay << endl;
}

Edited 3 Years Ago by Dani: Formatting fixed

>Help is greatly appreciated.
Which parts are you having problems with? In the code you posted, tryAgain is missing a closing brace for the function body, so it doesn't compile. After that it does, but there are quite a few warnings ranging from type mismatches to unused parameters to uninitialized variables to execution paths that don't return a value.

I would rewrite your code to correct these things and explain why I did what I did, but it still would not work correctly with your restriction that the function return values and parameter lists cannot change. The biggest problem is in passing variables by value, but using them as if they were passed by reference. So my second question is why can't you change the function declarations when they seem to be wrong?

Narue,
I can pass by address, not by reference. I appreciate any help you can give. Thank you for responding. Paul

Narue,
I can pass by address, not by reference. I appreciate any help you can give. Thank you for responding. Paul

Just to clarify, passing by value is this:

void f ( int param );

Passing by address is this:

void f ( int *param );

And passing by reference is this:

void f ( int& param );

The problem with your code is that you declare functions to pass by value, but you then seem to expect the same effect as if you passed by reference or address. The most notable statement that you made, and the one that confuses me the most is this one:

and all functiontions must keep the header type below.

I read this as meaning that the function return type and parameter lists could not be changed. Is that true or not? Because this is wrong (any anything similar):

int ID, <snip>; // ID is uninitialized

...

// ID is still uninitialized, you seem to expect to pass it by reference here
result = getEmployeeData (ID, pyrllSt, quit, Salary, hourwrk, sales, grossPay);

...

// ID is still uninitialized. At this point you will invoke undefined behavior
displayEmployeeInfo (ID, pyrllSt, grossPay);

...

// Once again, undefined behavior.
calculateGrossPay(ID, pyrllSt, Salary, hourwrk, sales, grossPay);

The problem could easily be fixed if you change getEmployeeData to take references for any variables that it gives values to and the code expects to be initialized when the function returns:

bool getEmployeeData (int&, char&, char&, int&, int&, int&, int);

In fact, you can tighten up the code considerably by decoupling these functions and removing redundancies:

#include <iostream>

using namespace std;

double calc_gross_pay ( int totals, char status );
void employee_results ( int id, char status, double gross_pay );

int main()
{
  bool done = false;

  while ( !done ) {
    int id, totals;
    double gross_pay;
    char status;

    cout<<"Enter employee ID, 0 to quit: ";
    cin>> id;
    cin.ignore(); // Eat newline

    if ( id == 0 )
      done = true;
    else {
      cout<<"Enter payroll status: ";
      cin.get ( status );
      cin.ignore(); // Eat newline

      switch ( status ) {
      case 'S':
      case 's':
        cout<<"Enter monthly salary: ";
        break;
      case 'H':
      case 'h':
        cout<<"Enter hours worked this month: ";
        break;
      case 'C':
      case 'c':
        cout<<"Enter total monthly sales: ";
        break;
      default:
        cerr<<"Invalid input, bailing"<<endl;
        done = true;
        break;
      }

      cin>> totals;
      gross_pay = calc_gross_pay ( totals, status );

      employee_results ( id, status, gross_pay );
    }
  }
}

double calc_gross_pay ( int totals, char status )
{
  switch ( status ) {
  case 'S':
  case 's':
    return totals;
  case 'H':
  case 'h':
    return totals * 18.75;
  case 'C':
  case 'c':
    return totals * .06 + 1000.00;
  default:
    return 0.0;
  }
}

void employee_results ( int id, char status, double gross_pay )
{
  cout<<"Employee ID:       "<< id <<endl;
  cout<<"Payroll Status:    "<< status <<endl;
  cout<<"Gross monthly pay: "<< gross_pay <<endl;
}
This article has been dead for over six months. Start a new discussion instead.