2014-04-06 37 views
0

我在理解構造函數時遇到了問題,並且在顯示帶有字符串名稱和字符串位置的數據時導致出現問題。名稱和位置的第一個字符被刪除並用「」(空格)初始化。數據顯示和構造函數問題

請忽略任何壓痕問題等,因爲這是我第一次在這裏張貼...

#include <cstdlib> 
#include <iostream> 
#include <string> 

using namespace std; 


class Employee 
{ 
    private: 
     string name ; // for employee's name 
     int id ;  // for employee's id number 
     string department ; // for name department 
     string position ; //for position employee holds 

    public: 

     Employee() // constructor 
     { 
      name = " " ; 
      id = 0 ; 
      department = " " ; 
      position = " " ; 
     } 

     Employee (char a , int i) 
     { 
      name = a ; 

      id = i ; 

      position = " " ; 

      department = " " ; 

     } 

     Employee (char a , char b , char c , int i) 
     { 
      name = a ; 

      id = i ; 

      department = b ; 

      position = c ; 


     } 

     Employee (const Employee &obj) 
     { 
      name = obj.name ; 

      department = obj.position ; 

      position = obj.position ; 

      id = obj.id ; 

     } 

     int set_Name(char a) 
     { 
      name = a ; 

      return 0; 

     } 

     int set_Id(int i) 
     { 
      id = i ; 

      return 0; 

     } 

     int set_Position(char b) 
     { 
      position = b ; 

      return 0; 

     } 

     int set_Department(char d) 
     { 
      department = d ; 

      return 0; 
     } 



     string get_Name(void) const 
     { 
      return name ; 

     } 

     int get_Id() const 
     { 
      return id ; 

     } 

     string get_Position(void) const 
     { 
      return position ; 

     } 

     string get_Department(void) const 
     { 
      return department ; 

     } 



     int set_Info(char a , char b , char c , int i) 
     { 
      name = a ; 

      id = i ; 

      department = b ; 

      position = c ; 

      return 0; 
     } 

     void get_Info() 
     { 
      cout << endl << " Enter Employee name = " ; 
      cin.ignore(); 
      getline(cin , name); 

      cout << " Enter id number of employee = " ; 
      cin >> id ; 


      cout << " Enter Employee department = " ; 
      cin.ignore(); 
      getline(cin , department); 

      cout << " Enter Emloyee position in company = "; 
      cin.ignore(); 
      getline(cin , position); 


     } 

     void put_Info() 
     { 
      cout << "Employees info : - " ; 
      cout << " " << name << " " << id << " " << department << " " << position << endl ; 

     } 

     ~Employee() 
     { 
      cout << " destructor executed .... " ; 
     } 

}; 


int main() 
{ 

    Employee obj1 , obj2 , obj3 , obj4 , obj5 ; 

    obj1.get_Info(); 
    obj2.get_Info(); 
    obj3.get_Info(); 
    obj4.get_Info(); 
    obj5.get_Info(); 

    cout << endl; 

    obj1.put_Info(); 
    obj2.put_Info(); 
    obj3.put_Info(); 
    obj4.put_Info(); 
    obj5.put_Info(); 

    return 0; 
} 
+0

您正在使用'char'作爲您的許多函數的參數類型而不是'string'。你的意思是? –

回答

0

cin.ignore();

使用cin >> name;cin >> department;當您使用getline看到here

您可以刪除 cin.ignore()