2014-04-06 2 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 >> name; или cin >> department; перед cin.ignore();

Вы можете удалить cin.ignore() при использовании getlinehere см

Смежные вопросы