运行失败(退出值1)c ++

时间:2012-01-24 09:25:31

标签: c++ netbeans

我正在为员工数据构建一个程序,由于某些原因我的代码无法运行,我搜索了这个论坛和其他人,我无法弄清楚我的代码是否存在问题。

#include <cstdlib>
#include <iomanip>
#include <iostream>

using namespace std;


class Employee{
    public:

        int idNumber;
        float SalaryRate;
        char * name;
        int BaseSalary;
        char * hisname;
        float salary;
        float bonus;
        float finalSalary;

        Employee(int idNum) //default constructor function
        {
            SalaryRate=0;
            BaseSalary=0;
            idNumber=idNum;
            BaseSalary=0;
            salary=0;
            bonus=0;        
        }
        //constructor function with parameters
        Employee(char * name, int SalaryRate, int idNumber)
        {

            SalaryRate=0;
            idNumber=0;
            strcpy(name, hisname) ;
        }

        float setBonus()
        {
            cout<<"What is the bonus for this employee?\n";
            cin>>bonus;

        }

        void increaseSalary (float increase)
        {
            cout<<"By what percentage would you like to increase ";
            cout<<"p";
            cout<<"'s salary? \n";
            cin>>increase;
            finalSalary = salary * (increase/100)+bonus;
        }


        void print ()
        {
            cout<<"the salary of ";
            cout<<* name;
            cout<< " is "; 
            cout<<finalSalary; 
        }
};


int main() {
    Employee * employees[100];

    for(int i = 0; i < 100; i++)
    {
        cout<<"What is the name you would like to input? ";
        cin>>employees[i]->name;
        int idNumber=i;
        cout<<"What is "; employees[i]->name; "'s hourly rate? ";
        cin>>employees[i]->SalaryRate;       
    }

    //Employee a();
    //a.increaseSalary();

    return 0;
}

5 个答案:

答案 0 :(得分:2)

您正在为员工分配100个指针。但这些还没有建成。

Employee* employees[100];

for(int i = 0; i < 100; i++)
{
    Employee* emp = new Employee(i);
    cout<<"What is the name you would like to input? ";
    cin >> emp->name;
    int idNumber=i;
    cout << "What is "; emp->name; "'s hourly rate? ";
    cin >> emp->SalaryRate;

    employees[i] = emp;
}

答案 1 :(得分:1)

指针数组employees[i]未分配任何内存 你需要分配带内存的指针才能以有意义的方式使用它们 此外,
您正在尝试将数据写入未分配的指针,从而导致未定义的行为 您需要使用name为指针new分配足够的内存来保存您输入的字符串。

此外,您需要按照课程的 Rule of Three 进行操作。

答案 2 :(得分:1)

您没有初始化您的Employee * employees[100];或员工中的字符串。

你想要的可能是:

class Employee{
public: 
    int idNumber;
    float SalaryRate;
    std::string name; // <--- !
    int BaseSalary;
    std::string hisname; // <--- !
    float salary;
    float bonus;
    float finalSalary;  
...
};
int main() {
    Employee employees[100]; // <--- !

    for(int i = 0; i < 100; i++)
    {
        cout<<"What is the name you would like to input? ";
        cin>>employees[i].name;
        int idNumber=i;
        cout<<"What is "; employees[i].name; "'s hourly rate? ";
        cin>>employees[i].SalaryRate;
    }

    //Employee a();
    //a.increaseSalary();

    return 0;
}

答案 3 :(得分:1)

我看到了几个问题:

  • 未分配您的员工(如其他答案中所述)
  • 期待cout<<"What is "; employees[i]->name; "'s hourly rate? ";打印您想要的内容。这实际上是三个单独的陈述。要打印全部三个,请使用cout << "What is " << employees[i]->name << "'s hourly rate? ";
  • 使用c风格的字符串而不是std::string
  • 通过使Employee成员公开
  • 来打破封装

可能存在其他问题,这些是我先找到的问题。

答案 4 :(得分:0)

它很快就崩溃了。

这是因为:

Employee * employees[100];

声明一个包含100个员工指针的数组。不是对象。

然后在循环中尝试访问不存在的对象:

employees[i]->name

因为您是通过尚未初始化的指针进行访问的 在开始使用指针和动态分配的对象之前,您需要了解对象。

Employee     employees[100];   // Declare an array of 100 objects.

然后你可以用以下内容读取名字:

cin >> employees[i].name;

但现在你遇到的问题是name是一个整体指针。问题继续存在。您需要从代码中删除指针并尽可能使用objets。