显示时输出不正确

时间:2014-10-22 22:54:23

标签: c++ class output

我的输出名称未显示在我的程序中。我一直在看代码和 我只是找不到我的错误

输入

name : John Dough
id : 123445
start date : 10312014
shift: 2

输出

name : ^^^^^^    <<<< I am having problem my name not being displayed
id : 123445
start date : 10312014
shift : 2

//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;
 }

3 个答案:

答案 0 :(得分: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;
}

我会通过将EmpName(也是错误的,单个字符)更改为std::string

来使用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();
};

也不要忘记在主要功能中将char name[100]更改为std::string

Live Example

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

答案 1 :(得分:1)

您的代码存在多个问题。

首先,Employee :: EmpName的数据类型不应该是char。它应该是一个char数组,或者更好的是std :: string。

其次,setEmpName函数的参数应该是const char *或const std :: string&amp;。

第三,name变量应该是std :: string而不是char数组。当然,如果你做了那个改变,setEmpName函数的参数应该是const std :: string&amp;。

第四,在调用setEmpName函数时,你应该按如下方式调用它:info.setEmpName(name)。

接下来,你应该使用std :: getline(cin,name)而不是cin.getline(name,100)。

答案 2 :(得分:0)

我首先纠正你的错误,然后给你一个更好的替代解决方案。

您的类的成员和setter函数的参数只是一个char 。将它们更改为数组:

char EmpName[100];

void setEmpName(char[]);

并且在实现中,您需要将给定数组的内容复制到您的成员:

void Employee::setEmpName(char[] x) {
    memcpy(EmpName, x, 100);
}

然而,这是 C方式

C ++执行此操作的方法是使用std::string。为此,请更改成员的类型,setter和getter中的参数以及main中的std::string类型。要阅读std::string,请使用getline的独立重载,它只接收流和字符串(但不计数):

getline(cin, name);