c++ pointers with overloaded functions and user input

61 Views Asked by At

I'm getting multiple errors.. I've tried this with different variables and different data types. I clearly don't understand how to use pointers properly. I am not looking for someone to give me the answer, I would like to learn more and figure it out myself if possible however I am stuck - any advice is greatly appreciated.

I am always upfront - this is for an assignment for school. I have completed and met all of the other grading requirements, except the requirement of using at least one example of a pointer in my function.

The example here, I am trying to use a pointer with double employeeSalary in main and in case 2 as well. Earlier I was trying to use them with my function prototypes, but that didn't seem to work either. I am getting Exception thrown at 0x00007FFB0897EBCE (msvcp140d.dll) in ConsoleApplication5.exe: 0xC0000005: Access violation writing location 0x0000000000000000.

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

double grossPay(const int number, double hours, double pay); // hourly function
double grossPay(int number, double salary); // salary function
double grossPay(int company, int project, int number, double hours, double pay);  // contract function
double grossPay(int institution, int department, double hours, double pay); // intern function


int main() {
    // prompt user for type of employee and give EOF instructions.
    cout << "Enter '1' for hourly. Enter '2' for salaried." << endl;
    cout << "Enter '3' for contracter. Enter '4' for intern." << endl;
    cout << endl;
    cout << "Terminate input by using <ctrl> z on Windows then press enter." << endl;
    cout << "Terminate input by using <ctrl> z on UNIX / Linux / Mac OS X then press enter." << endl;

    int employeeType{ 0 };

    int employeeNumber{ 0 };
    double overtimePay{ 0 };

    // salaried
    double *employeeSalary{ 0 };

    // hourly
    double hoursWorked{ 0 };
    double payRate{ 0 };

    // contractor
    int companyNum{ 0 };
    int projectNum{ 0 };

    // intern
    int schoolCode{ 0 };
    int departmentCode{ 0 };

    while (cin >> employeeType) {

        switch (employeeType) {
        case 1:
            // HOURLY employee prompts and output
            cout << "Enter employee number: " << endl;
            cin >> employeeNumber;
            cout << "Enter number of hours employee worked: " << endl;
            cin >> hoursWorked;
            cout << "Enter employees pay rate: " << endl;
            cin >> payRate;

            while (hoursWorked > 50 || hoursWorked < 0) {
                hoursWorked = -1;

                cout << "Input invalid, please enter acceptable work hours for hourly employee (0-50):" << "\n" << endl;

                cin >> hoursWorked;

            }

            if (hoursWorked > 0 || hoursWorked < 50) {
                cout << setprecision(2) << fixed;
                cout << "Gross pay of employee #" << employeeNumber << " is $" << grossPay(employeeNumber, hoursWorked, payRate) << endl;
                cout << endl;
            }


            break;

        case 2:
            // SALARIED employee prompts and output
            cout << "Enter employee number: " << endl;
            cin >> employeeNumber;
            cout << "Enter employees salary: " << endl;
            cin >> *employeeSalary;
            
            cout << setprecision(2) << fixed;
            cout << "Gross pay of employee #" << employeeNumber << " is $" << grossPay(employeeNumber, *employeeSalary) << endl;
            cout << endl;

            break;

        case 3:
            // CONTRACT employee prompts and output
            cout << "Enter company number: " << endl;
            cin >> companyNum;
            cout << "Enter project number: " << endl;
            cin >> projectNum;
            cout << "Enter employee number: " << endl;
            cin >> employeeNumber;
            cout << "Enter number of hours employee worked: " << endl;
            cin >> hoursWorked;
            cout << "Enter employees pay rate: " << endl;
            cin >> payRate;

            while (hoursWorked > 40 || hoursWorked < 0) {
                hoursWorked = -1;

                cout << "Input invalid, please enter acceptable work hours for contract employee (0-40):" << "\n" << endl;
                cin >> hoursWorked;
            }

            if (hoursWorked > 0 || hoursWorked < 40) {
                cout << setprecision(2) << fixed;
                cout << "Gross pay of contractor #" << employeeNumber << " is $" << grossPay(companyNum, projectNum, employeeNumber, hoursWorked, payRate) << endl;
                cout << endl;
            }

            break;

        case 4:
            // INTERN prompts and output
            cout << "Enter institution code: " << endl;
            cin >> schoolCode;
            cout << "Enter department code: " << endl;
            cin >> departmentCode;
            cout << "Enter number of hours employee worked: " << endl;
            cin >> hoursWorked;
            cout << "Enter employees pay rate: " << endl;
            cin >> payRate;

            while (hoursWorked > 20 || hoursWorked < 0) {
                hoursWorked = -1;

                cout << "Input invalid, please enter acceptable work hours for an intern (0-20):" << "\n" << endl;
                cin >> hoursWorked;
            }

            if (hoursWorked > 0 || hoursWorked < 20) {
                cout << setprecision(2) << fixed;
                cout << "Gross pay of the intern is $" << grossPay(schoolCode, departmentCode, hoursWorked, payRate) << endl;
                cout << endl;
            }

            break;
        }

        cout << "Enter '1' for hourly. Enter '2' for salaried." << endl;
        cout << "Enter '3' for contracter. Enter '4' for intern." << endl;
        cout << endl;

    }

    cout << endl;
    cout << "Thank you for using this program. " << endl;

}


// validation in main code
// hourly function
double grossPay(const int number, double hours, double pay) {
    double hourlyWeek{ 0 };

    if (hours > 40.00) {
        hourlyWeek = (pay * 40.00) + ((hours - 40.00) * (pay * 1.50));
    }

    else {
        hourlyWeek = (hours * pay);
    }

    return hourlyWeek;
}

// salary function
double grossPay(int number, double salary) {
    double salaryWeek{ 0 };

    salaryWeek = (salary/52.00);

    return salaryWeek;
}

//contractor function
double grossPay(int company, int project, int number, double hours, double pay) {
    double contractWeek{ 0 };

    contractWeek = (hours * pay);

    return contractWeek;

}

// intern function
double grossPay(int institution, int department, double hours, double pay) {
    double internWeek{ 0 };

    if (hours > 15.00) {
        internWeek = (pay * 15) + ((hours - 15) * (pay * 1.25));
    }

    else {
        internWeek = (hours * pay);
    }


    return internWeek;
}
1

There are 1 best solutions below

0
On

If you reduce your code to the simplest example that reproduces the error, you get this:

int main() {
  // salaried
  double *employeeSalary{ 0 }
  *employeeSalary = 1000;

  return 0;
}

You have a pointer, and you "dereference" it (which is to say that you try to use the thing it points to). But it doesn't point to anything. You try to write to memory space that doesn't belong to you, and you get an error. You must point the pointer to something:

int main() {
  // salaried
  double salary(0);
  double *pSalary = &salary;

  *pSalary = 1000;

  return 0;
}