在C ++中使用动态数组时遇到问题

时间:2017-12-03 21:44:49

标签: c++ arrays dynamic-arrays

我正在为我的编程课做这个项目,而且我很难理解我应该如何使用动态数组和类。我的想法是,我应该允许用户输入他们想要创建的工资单记录数量。当我运行程序时,一切似乎都顺利进行,直到我告诉程序我没有更多的员工要为那个工资单输入,此时程序通常会关闭。相反,它会崩溃并创建一个查看delete []工资单的断点;主线。因为它指的是那条线,我假设我没有错误地创建动态数组但是我不确定我应该改变什么以及在哪里。感谢我能得到的任何帮助或提示。

这是我的Payroll类的Header文件

#ifndef HEADER_H
#define HEADER_H
#include <iostream>
#include <string>
using namespace std;

class Payroll
{
    private:
    int empNum;
    static int total_payrolls;
    double pay;
    double hours;
    string firstname;
    string lastname;

public:

    Payroll();

    Payroll(int en, double p, double h, string fn, string ln);

    ~Payroll();

    int getEmpNum();
    double getPay();
    double getHours();
    string getFirstname();
    string getLastname();
    double getTotalPay();

    void setEmpNum(int);
    void setPay(double);
    void setHours(double);
    void setFirstname(string);
    void setLastname(string);

};
#endif

这是我的工资单类文件

#include "Header.h"
using namespace std;

Payroll::Payroll()
{
    int empNum = 0;
    int total_payrolls = 0;
    total_payrolls++;
    double pay = 0.0;
    double hours = 0.0;
}

Payroll::Payroll(int en, double p, double h, string fn, string ln)
{
    empNum = en;
    pay = p;
    hours = h;
    firstname = fn;
    lastname = ln;
}

Payroll::~Payroll()
{

}

int Payroll::getEmpNum()
{
    return empNum;
}

double Payroll::getPay()
{
    return pay;
}

double Payroll::getHours()
{
    return hours;
}
string Payroll::getFirstname()
{
    return firstname;
}

string Payroll::getLastname()
{
    return lastname;
}

void Payroll::setEmpNum(int en)
{
    empNum = en;
}

void Payroll::setPay(double p)
{
    pay = p;
}
void Payroll::setHours(double h)
{
    hours = h;
}

void Payroll::setFirstname(string fn)
{
    firstname = fn;
}

void Payroll::setLastname(string ln)
{
    lastname = ln;
}

double Payroll::getTotalPay()
{
    return (pay * hours);
}

这是我的主要

#include <iostream>
#include <string>

#include <iomanip>
#include <fstream>
#include <ctype.h>
#include "Header.h"
using namespace std;


int main()
{

    string first_name;
    string last_name;
    int employee_number;
    int choice1;
    int choice2;
    int capacity;
    int num_of_employees;
    double pay_rate;
    double hours_worked;
    char doAgain;
    ofstream outputFile;
    outputFile.open("cop2224_proj2.txt ");


    Payroll *payroll;


    cout << "How many payrolls would you like to enter? ";
    cin >> capacity;
    payroll = new Payroll [capacity];

    cout << "Welcome to the employee payroll program \n";
    cout << "The purpose of this program is to collect employee \n";
    cout << "Payroll information, calculate that information \n";
    cout << "and store the information on a file \n";
    cout << "Please select an option below \n";
    cout << endl;
    cout << "1. Enter employee payrate information \n";
    cout << "2. Quit program \n";
    cout << "Enter your choice \n";
    cin >> choice1;


    do
    {

        while ((choice1 < 1) || (choice1 > 2))
        {
            cout << "Invaild input, only enter 1 or 2 into the keyboard \n";
            cin >> choice1;
        }

        if (choice1 != 2)
        {
            cout << "You have selected to enter the program \n";


            cout << "----------------------------------------- \n";


            cout << "Enter your 6 digit employee number \n";
            cin >> employee_number;
            while ((employee_number < 99999) || (employee_number > 1000000))
            {
                cout << "Only enter a number consisting of 6 digits \n ";
                cin >> employee_number;
            }

            cout << "Enter your first name \n";
            cin >> first_name;
            cout << "Enter your last name \n";
            cin >> last_name;
            cout << "Enter your payrate \n";
            cin >> pay_rate;

            cout << "Enter the amount of hours worked \n";
            cin >> hours_worked;

            payroll[capacity].setHours(hours_worked);
            payroll[capacity].setFirstname(first_name);
            payroll[capacity].setLastname(last_name);
            payroll[capacity].setHours(hours_worked);
            payroll[capacity].setPay(pay_rate);


            cout << payroll[capacity].getEmpNum() << ',' << endl;
            cout << payroll[capacity].getFirstname() << ',' << endl;
            cout << payroll[capacity].getLastname() << ',' << endl;
            cout << payroll[capacity].getPay() << ',' << endl;
            cout << payroll[capacity].getHours() << ',' << endl;
            cout << payroll[capacity].getTotalPay() << ',' << endl;

            cout << "Do you want to imput another employee's information? (Y/N)";
            cin >> doAgain;
        }


    } while (doAgain == 'Y' || doAgain == 'y');


    delete [] payroll;
    payroll = 0;
    outputFile.close();
    return 0;
}

0 个答案:

没有答案