1

I'm trying to add variables by users input using >> or getline but it is giving null values with both of them .As we know getline does not ignore leading whitespace characters that's why i'm using string whitespaces like employeeName=" "; string designation=" ";. Can anyone help me for getting proper output

class Employee{
  public:
  int employeeId,salary;
  string employeeName=" ";
  string designation=" ";
  void getEmployee(){
    cin>>employeeId;
    getline(cin,employeeName);
    getline(cin,designation);
    cin>>salary;
  }
  void ShowEmployee(){
    cout<<"Employee Id="<<employeeId<<"\n";
    cout<<"Employee Name="<<employeeName<<"\n";
    cout<<"Designation="<<designation<<"\n";
    cout<<"Salary="<<salary<<"\n";
  }
};
int main() {
  Employee ob;
  ob.getEmployee();
  ob.ShowEmployee();
}

enter image description here can anyone help me :)

krishna veer
  • 412
  • 1
  • 3
  • 11
  • 5
    Voting to close as typo. You forgot to ever call `getEmployee()` and you didn't actuall put the variables to output in your `cout< – walnut Apr 01 '20 at 06:17
  • 2
    And in anticipation of the next problem, please read [c++ getline() isn't waiting for input from console when called multiple times](https://stackoverflow.com/questions/21567291/why-does-stdgetline-skip-input-after-a-formatted-extraction). – walnut Apr 01 '20 at 06:20
  • 1
    Sorry i forget those things ,please check the edited code and please help me getting proper output. – krishna veer Apr 01 '20 at 08:37
  • 2
    It would help if you could also post your input text. – Paul Floyd Apr 01 '20 at 08:54
  • 1
    yes i had uploaded ,please check it – krishna veer Apr 01 '20 at 10:12

3 Answers3

1

But you are not calling getEmployee in your example. Did you forget?

You can also try a constructor passing the std::istream& to it like

Employee(std::istream& in) {
  in >> this->employeeId;
  in >> this->salary;
  ....
}
  • Sorry i forget to call getEmployee here , I also tried this using 'this' constructor but it is giving same null values .Please check the edited code and help me. – krishna veer Apr 01 '20 at 08:36
  • Not a problem. For a very quick and very dirty approach you can go with a cin.ignore(); before your first std::geline(...) that will allow to enter lines terminated with a NL. – Daniel Mortara Apr 01 '20 at 09:08
  • As @Kaldrr pointed before, it would be better not to mix the operator >> and getlline() It looks like you can read all the information to std::string using getline and convert those that need to be numbers instead. For instance using this approach [std:stringstream](http://www.cplusplus.com/forum/articles/6046/) – Daniel Mortara Apr 01 '20 at 09:44
1

You're never calling the getEmployee member functions. As your Employee object is created on the stack, and employeeId and salary are not initialized, they contain garbage values inside them.

In the ShowEmployee member function you're only outputting the salary variable value, so I'm guessing this isn't exactly the code that you're compiling and testing for yourself.

As for your reasoning why you're using getline, I don't understand it at all, the string that's passed as the 2nd argument will be overwritten, so whatever was in it before the call is lost. But if you want to mix operator>> and getline calls on the same stream like you do, you need to use ignore, as operator>> won't extract '\n' from the stream, which will be read by getline.

Kaldrr
  • 2,389
  • 4
  • 11
1
getline(cin,employeeName);
getline(cin,designation);

In the code above you are overwriting the string,If you want to mix users input of multiple datatypes try to use cin.ignore; before your first std::getline(...).