2012-01-24 83 views
-4

我正在構建員工數據的程序,出於某種原因,我的代碼無法運行,我搜索了此論壇和其他人,並且我無法弄清楚我的代碼存在問題。運行失敗(退出值1)C++

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

你有什麼樣的問題?如果你不能編譯它,什麼錯誤消息給編譯器? –

回答

1

指針陣列employees[i]沒有分配任何內存。
您需要分配帶有內存的指針才能夠以有意義的方式使用它們。
此外,
您正試圖將數據寫入未分配的指針,導致未定義的行爲
您需要使用new爲指針name分配足夠的內存以保存您輸入的字符串。

此外,您需要按照Rule of Three爲您的班級。

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

您並未初始化您的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; 
} 
+0

如何初始化員工*員工[100]和字符串? – user1166637

+0

我剛給你看,在我的代碼中它是自動的。 'std :: string'構造函數處理它。而「僱員」現在是一組自動初始化的員工,而不是像之前向員工提供的一系列指針。 – ronag

+0

我知道我沒有正確使用指針,所以我想了解如何使用它們 - 我如何初始化指針? – user1166637

1

我看到一對夫婦的問題:

  • 不分配你的員工(在其他的答案指出)
  • 期待cout<<"What is "; employees[i]->name; "'s hourly rate? ";,打印您想。這實際上是三個單獨的陳述。要打印所有三個,用c風格的字符串,而不是std::string
  • 通過使員工的成員公共

有可能的其他問題破壞了封裝用cout << "What is " << employees[i]->name << "'s hourly rate? ";

  • ,那些是我先找到的人。

  • 0

    它很快就崩潰了。

    這是因爲這樣的:

    Employee * employees[100]; 
    

    聲明100個僱員指針的數組。不是對象。

    然後在循環中,您試圖訪問的對象不存在:

    employees[i]->name 
    

    因爲你是通過尚未初始化的指針訪問。
    在開始播放指針和動態分配的對象之前,您需要了解對象。

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

    然後你就可以用閱讀的名字:

    cin >> employees[i].name; 
    

    但是現在你有一個名字是一個未初始化指針問題。問題繼續如此。你需要從你的代碼中刪除指針並在任何地方使用objets。

    相關問題