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?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
jasonclarkeCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.