1

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;
}
Tamara
  • 27
  • 1
  • 7
  • 3
    That is a null pointer dereference, and without question the easiest way to locate it is by running your program in a *debugger*. If you're using visual studio it has arguably the absolute best user-mode debugger on earth for the Windows platform. Should be trivial to use. Hint: `cin >> *employeeSalary;` is trying to read into the `double` pointed to by `employeeSalary`. So, what does that point to at the time that line executes? Look at your code and see where you set its address (if at all... and if *not*... well... that's a problem). – WhozCraig Mar 27 '22 at 23:31
  • 2
    Here is a vital rule that never seems to be taught in computer science courses: develop new functionality *in isolation* as mush as possible. Don't try to write pointer-handling code in the middle of a large preexisting program; write a small program that demonstrates pointer-handling and nothing else. Get it working perfectly before you try to integrate it into a larger body of code. – Beta Mar 27 '22 at 23:35
  • @Beta Second that. Things like Hoare, Dijkstra, Wirth, et'al publications on development practices and stepwise-refinement are nearly always overlooked in modern CS, and it's a f'ing shame as far as I'm concerned. Best CS course I ever took was an optional elective my senior year, Great Papers in Computer Science, and for what I first expected to be a 'fluff' class to fill an elective slot, it literally changed the direction of my entire engineering career. Practices like what you described were a mainstay in most of the best publications. – WhozCraig Mar 27 '22 at 23:44
  • A [mre] is another valuable debugging tool (not to mention a requirement for questions seeking debugging help), with emphasis on "minimal". Comment out about the second half of your code; see if the error disappears. If so, you've narrowed down where there error is. If not, try commenting out the other half. Repeat until removing any more code causes the error to no longer be reproducible. (Also, don't require user input for most MREs; hardcode whatever values are needed.) – JaMiT Mar 27 '22 at 23:55

1 Answers1

2

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;
}
Beta
  • 96,650
  • 16
  • 149
  • 150