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

run failed (exit value 1) c++

本文关键字:c++ 退出 失败 运行      更新时间:2023-10-16

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

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

您正在为一名员工分配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;
}

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

此外,您需要在课堂上遵守三条规则

您既没有初始化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;
}

我看到了几个问题:

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

可能还有其他问题,这些都是我最先发现的。

它崩溃得很快。

这是因为:

Employee * employees[100];

声明一个由100个员工指针组成的数组。NOT对象。

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

employees[i]->name

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

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

然后你可以用读取名字

cin >> employees[i].name;

但现在您遇到的问题是名称是一个统一的指针。问题就这样继续着。您需要从代码中删除指针,并尽可能使用对象。