RunUO Community

This is a sample guest message. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your own topics and posts, as well as connect with other members through your own private inbox!

Problem with getline

Axle

Wanderer
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
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.
 

noobie

Wanderer
umm,

"here" its a link, I think you didnt catch it :)

there is a suggested solution in that link.

and I am saying you could try to flush cin as another solution.

Code:
cin.flush();
getline(cin, somestring);
 

Axle

Wanderer
noobie;631101 said:
umm,

"here" its a link, I think you didnt catch it :)

there is a suggested solution in that link.

and I am saying you could try to flush cin as another solution.

Code:
cin.flush();
getline(cin, somestring);

Oh, well first let me thank you for the assistance. As far as the link it's not working.
 

Axle

Wanderer
Oh I see. Unfortunately that doesn't seem to be the problem. It completely ignores the getline(cin, name) statement as it doesn't allow for any input and goes directly to the next statement.
 

Axle

Wanderer
noobie;631129 said:
well, did you try my suggestion?

using cin.flush() before getline?

The IStream class doesn't have a flush member, it does have a clear() member though, of which I did try, but did not work. The first getline works properly (when you're prompted for the employee name). But it skips the second getline command entirely.

Note: The program was written and compiled using VS 2005 SP1
 

Axle

Wanderer
noobie;631139 said:
well i dont have any compiler right now, so I cant really try it.

but istream do have a flush function.

http://www.cppreference.com/cppio/all.html

clear function doesnt do same thing.

std::istream does not have a flush member. I think flush is a member of fstream. At any rate, the extra character did seem to be the problem, and I fixed with placing a cin.ignore() statement just before the second getline statement. It works now! Thanks for the help and pointing me in the right direction! I've been stuck on this for some time now, and now I can finally push foreward! Thanks again (+karma!).

Code:
void Person::GetSurveyInfo()
{
	string name;

	cout << "Enter the name of the survey: " << flush;
	cin.ignore();
	getline(cin, name);

	Survey survey = *(new Survey(GetSurveyName()));
	cin >> survey;
	AddSurvey(survey);
}
 
Top