Help

Below is my program.  I don't know it doesn't allow me input
second employee name.  Please tell me.  Thank you.  



#include <iostream.h>
#include <string.h>

class Employee
{
      private :
            char *name;
            int age;
            int YearsOfService;
            float Salary;

      public :
            Employee();
            void setEmployeeDetail();
            void setEmployeeName(char*);
            void setAge(int);
            void setYearsOfService(int);
            void setSalary(float);
            void display();
};

Employee::Employee()
{
  name = "";
  age = 0;
  YearsOfService = 0;
  Salary = 0;
}


void Employee::setEmployeeDetail()
{
      const int Max = 80;
      char eName[Max];
      int eAge, eYOS;
      float eSalary;

      cout << "Enter employee name : ";
      cin.getline(eName, Max);

      cout << "Enter age : ";
      cin >> eAge;

      cout << "Enter salary : $";
      cin >> eSalary;

      cout << "Enter Years of service : ";
      cin >> eYOS;

      setEmployeeName(eName);
      setAge(eAge);
      setSalary(eSalary);
      setYearsOfService(eYOS);
}

void Employee::setEmployeeName(char *eName)
{
      name = new char[strlen (eName) + 1];
      strcpy(name, eName);
}

void Employee::setAge(int x)
{
      age = x;
}

void Employee::setYearsOfService(int x)
{
      YearsOfService = x;
}

void Employee::setSalary(float x)
{
      Salary = x;
}

void Employee::display()
{
      cout << "Employee name : " << name;
      cout << endl;
      cout << "Age : " << age;
      cout << endl;
      cout << "Salary : $" << Salary;
      cout << endl;
      cout << "Year of service : " << YearsOfService;
      cout << endl;
}


void main()
{
      int i;
      Employee erecord[2];

      for (i = 0; i < 2; i++)
            erecord[i].setEmployeeDetail();

      for (i = 0; i < 2; i++)
            erecord[i].display();
}
ylleeAsked:
Who is Participating?
 
jasonclarkeConnect With a Mentor Commented:
It seems to be because a carriage return is left on the input stream before the next call to getline.

Put

cin.ignore();

after:

cin << eYOS;

and it should work OK.
0
 
KangaRooCommented:
Somehow the second getline 'sees' a junk end-of-line in cin's buffer.
0
 
nietodCommented:
yllee, can you explain a little more?  What do you mean by " it doesn't allow me input second employee name"?
0
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 
jasonclarkeCommented:
Sorry, didn't see the comments you two made.  The stream doesn't wait for input on the second call to cin.getline(), it just carries on takes an carriage return character that seems to be left in the input stream, so you end up with an empty string being read.
0
 
nietodCommented:
jason, getline() will remove the \n.  The only time it would remain is if the max characters were read before the \n was reached.  
0
 
KangaRooCommented:
I've tried the code and the getline() will just go ahead and read an empty line if there was a regular extraction prior to it. As mentioned, as if the end-of-line remains in the buffer.
0
 
KangaRooCommented:
jason, ignore() will work in the example, but rewrite main:

for (i = 0; i < 2; i++)
{
cin >> somethingelse;
erecord[i].setEmployeeDetail();
}

// ...

You cannot guarantee the state of cin prior to the getline() like that.

Is there a compelling reason not to use the string class:
string eName;
cin >> ename;
setEmployeeName(eName.c_str());

You'd have to change setEmployeeName to accept a const char*
0
 
jasonclarkeCommented:
> for (i = 0; i < 2; i++)
> {
> cin >> somethingelse;
> erecord[i].setEmployeeDetail();
> }

the trouble with this is, presumably, that you would get a spurious wait for input on the first time around.  

is there any way to do an operation that is always 'non-blocking' on cin.  ignore() and peek() both seem to cause waits for input if called at the wrong moment.
0
 
ylleeAuthor Commented:
Thank you ver much.
0
All Courses

From novice to tech pro — start learning today.