简体   繁体   English

c++ 具有重载函数和用户输入的指针

[英]c++ pointers with overloaded functions and user input

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.除了在我的 function 中至少使用一个指针示例的要求外,我已经完成并满足所有其他评分要求。

The example here, I am trying to use a pointer with double employeeSalary in main and in case 2 as well.这里的例子,我试图在 main 和 case 2 中使用一个带有双倍 employeeSalary的指针。 Earlier I was trying to use them with my function prototypes, but that didn't seem to work either.早些时候我试图将它们与我的 function 原型一起使用,但这似乎也不起作用。 I am getting Exception thrown at 0x00007FFB0897EBCE (msvcp140d.dll) in ConsoleApplication5.exe: 0xC0000005: Access violation writing location 0x0000000000000000.我在 ConsoleApplication5.exe 的 0x00007FFB0897EBCE (msvcp140d.dll) 处抛出异常:0xC0000005:访问冲突写入位置 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;
}

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.您尝试写入不属于您的 memory 空间,并且出现错误。 You must point the pointer to something:您必须将指针指向某物:

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

  *pSalary = 1000;

  return 0;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM