显示时输出不正确

Eas*_*syE 2 c++ class output

我的输出名称未显示在我的程序中.我一直在看代码,我找不到我的错误

输入

name : John Dough
id : 123445
start date : 10312014
shift: 2
Run Code Online (Sandbox Code Playgroud)

产量

name : ^^^^^^    <<<< I am having problem my name not being displayed
id : 123445
start date : 10312014
shift : 2
Run Code Online (Sandbox Code Playgroud)

//This program demostrates a class and a derived class with constructors.
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
class Employee
{
private:
    char EmpName;
    int EmpNum;
    int HireDate;
public:
    void setEmpName(char);
    void setEmpNum(int);
    void setHireDate(int);
    char getEmpName() const;
    int getEmpNum() const;
    int getHireDate() const;
    Employee();
};
void Employee::setEmpName(char x)
{
    EmpName = x;
}
void Employee::setEmpNum(int y)
{
    EmpNum = y;
}
void Employee::setHireDate(int z)
{
    HireDate = z;
}
char Employee::getEmpName() const
{
    return EmpName;
}
int Employee::getEmpNum() const
{
    return EmpNum;
}
int Employee::getHireDate() const
{
    return HireDate;
}
Employee::Employee()
{
    cout << "I will ask you some questions about an employee.\n\n";
}
class ProductionWorker : public Employee
{
    private:
        int Shift;
        double HourlyPayRate;
    public:
        void setShift(int);
        void setHourlyPayRate(double);
        int getShift() const;
        double getHourlyPayRate() const;
        ProductionWorker();
};
void ProductionWorker::setShift(int a)
{
    Shift = a;
}
void ProductionWorker::setHourlyPayRate(double b)
{
    HourlyPayRate = b;
} 
int ProductionWorker::getShift() const
{
    return Shift;
}
double ProductionWorker::getHourlyPayRate() const
{
    return HourlyPayRate;
}
ProductionWorker::ProductionWorker()
{
    cout << "After answering the questions,\n";
    cout << "I will display the employee's information.\n\n\n";
}
int main()
{
    ProductionWorker info;
    char name[100];
    int num;
    int date;
    int shift;
    double rate;
    cout << "What is the employee's name? ";
    cin.getline(name, 100);

    cout << "What is the employee's number? ";
    cin >> num;

    cout << "What is the employee's hire date?\n";
    cout << "(Month, day, and year without any slashes,\n";
    cout << "dashes, commas, or other punctuation.)\n";
    cout << "For example, January 14, 1983 would look like 01141983. ";
    cin >> date;

    cout << "Does the employee work shift 1 or shift 2? ";
    cin >> shift;

    cout << "How much does the employee make per hour? ";
    cin >> rate;
    info.setEmpName(name[100]);
    info.setEmpNum(num);
    info.setHireDate(date);
    info.setShift(shift);
    info.setHourlyPayRate(rate);
    cout << "\n\nHere is the employee's data:\n\n";
    cout << "Employee's Name: " << info.getEmpName() << endl;
    cout << "Employee's Number: " << info.getEmpNum() << endl;
    cout << "Employee's Hire Date: " << info.getHireDate() << endl;
    cout << "Employee's Shift: " << info.getShift() << endl;
    cout << setprecision(2) << fixed;
    cout << "Employee's Hourly Pay Rate: $" << info.getHourlyPayRate() << endl << endl;
    return 0;
 }
Run Code Online (Sandbox Code Playgroud)

Mar*_* A. 5

这是错误的:您正在访问超出范围的字符,而不是将数组传递给函数

char name[100];
//.. initialize name..
info.setEmpName(name[100]); // Accesses the 100th character (out-of-range [0-99])

void Employee::setEmpName(char x)
{
    EmpName = x;
}
Run Code Online (Sandbox Code Playgroud)

我会std::string通过改变EmpName(也是错误的,它不是单个字符)来使用std::string

class Employee
{
private:
    string EmpName;
    int EmpNum;
    int HireDate;
public:
    void setEmpName(std::string& name);
    void setEmpNum(int);
    void setHireDate(int);
    string getEmpName() const;
    int getEmpNum() const;
    int getHireDate() const;
    Employee();
};
Run Code Online (Sandbox Code Playgroud)

另外不要忘记在主函数中更改char name[100]为a std::string.

Live Example

您当然可以使用char数组来完成此操作,在这种情况下,如果您打算使用固定大小的数组,您可以通过引用传递它,或者只是将指向数组的指针的内容复制到内存数组中Employee.