Problem with getline
In my program I'm writing, I'm trying to construct a person object and a survey object. Both of which take strings as a constructor parameter. I'm using getline to get the name of each object (so as to allow for multi-word names, I.E., John Doe). The problem I'm having is that when I use getline to get the person's name it works fine, but when I use it in a similar method to retrieve the survey name, it does't work.
EmployeeList.cpp
Person.cpp
In the GetSurveyInfo function of Person.cpp, it ignores the getline statement, which works fine in the AddEntry function of EmployeeList.cpp. Any help is greatly appreciated.
In my program I'm writing, I'm trying to construct a person object and a survey object. Both of which take strings as a constructor parameter. I'm using getline to get the name of each object (so as to allow for multi-word names, I.E., John Doe). The problem I'm having is that when I use getline to get the person's name it works fine, but when I use it in a similar method to retrieve the survey name, it does't work.
EmployeeList.cpp
Code:
#include "Controller.h"
#include "EmployeeList.h"
#include "Person.h"
list<Person> personList;
list<Person>::iterator iter;
EmployeeList::EmployeeList(const string &f):filename(f)
{
cout << "Constructed a List" << endl;
Sleep(500);
}
EmployeeList::~EmployeeList()
{
// code to handle destruction
}
void EmployeeList::AddEntry()
{
string name;
int id;
cout << "Please enter your name: " << flush;
getline(cin, name); [COLOR="SeaGreen"]// here it works fine[/COLOR]
cout << "Please enter your 5 digit empoyee I.D.: " << flush;
cin >> id;
personList.push_back( *(new Person(id, name)) );
}
void EmployeeList::GetEmployeeInfo()
{
}
void EmployeeList::AddEmployee()
{
}
Person.cpp
Code:
#include "Person.h"
#include "Controller.h"
list<Survey>surveyList;
list<Survey>::iterator iter;
Person::Person(int id, const string &n):name(n)
{
SetName(n);
SetId(id);
GetSurveyInfo();
}
Person::~Person()
{
}
ostream& operator<< (ostream& osObject, const Person& person)
{
osObject << person.name << " | " << person.empId << endl << endl;
return osObject;
}
//istream& operator>> (istream& isObject, Person& person)
//{
// cout << "Enter the name of the survey: " << flush;
// getline(isObject, survey.surveyName);
// cout << "Enter the actual hours worked: " << flush;
// isObject >> survey.actualHours;
// cout << "Enter the quota per hour of the survey: " << flush;
// isObject >> survey.quota;
// cout << "Enter your team's base pay: " << flush;
// isObject >> survey.teamBasePay;
//
// return isObject;
//}
void Person::SetName(string arg)
{
name = arg;
}
void Person::SetId(int arg)
{
empId = arg;
}
string Person::GetName()
{
return name;
}
int Person::GetId()
{
return empId;
}
void Person::GetSurveyInfo()
{
string name;
cout << "Enter the name of the survey: " << flush;
getline(cin, name); [COLOR="Red"]// here it doesn't work, and immediatly goes to the next (friend) function.[/COLOR]
Survey survey = (*(new Survey(name)));
cin >> survey;
AddSurvey(survey);
}
void Person::AddSurvey(Survey survey)
{
char ch;
system("cls");
cout << survey;
cout << "Is this information correct? (Y)es/(N)o?" << endl;
cin >> ch;
if (ch == 'N' || ch == 'n')
{
GetSurveyInfo();
}
else if(ch == 'Y' || ch == 'y')
{
surveyList.push_back( survey );
}
else
AddSurvey(survey);
Sleep(1000);
}
In the GetSurveyInfo function of Person.cpp, it ignores the getline statement, which works fine in the AddEntry function of EmployeeList.cpp. Any help is greatly appreciated.