Go Back   RunUO - Ultima Online Emulation > Developer's Corner > Programming > C/C++

C/C++ C/C++ Discussion

Reply
 
Thread Tools Display Modes
Old 01-06-2007, 03:32 PM   #1 (permalink)
Forum Expert
 
Axle's Avatar
 
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
Default 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);   // here it works fine
	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); // here it doesn't work, and immediatly goes to the next (friend) function.
	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.

Last edited by Axle; 01-06-2007 at 03:36 PM.
Axle is offline   Reply With Quote
Old 01-06-2007, 03:48 PM   #2 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

here

dumb bunny ahah

and try to flush cin not cout .. that might work too..

Code:
cin.flush()
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."

Last edited by noobie; 01-06-2007 at 03:50 PM.
noobie is offline   Reply With Quote
Old 01-06-2007, 04:01 PM   #3 (permalink)
Forum Expert
 
Axle's Avatar
 
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
Default

Quote:
Originally Posted by noobie View Post
here

dumb bunny ahah

and try to flush cin not cout .. that might work too..

Code:
cin.flush()
Not quite sure I follow you.
Axle is offline   Reply With Quote
Old 01-06-2007, 04:05 PM   #4 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

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);
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
noobie is offline   Reply With Quote
Old 01-06-2007, 04:09 PM   #5 (permalink)
Forum Expert
 
Axle's Avatar
 
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
Default

Quote:
Originally Posted by noobie View Post
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 is offline   Reply With Quote
Old 01-06-2007, 04:13 PM   #6 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

try my first post for the link..there is not in the second one..

or just use this one

http://mathbits.com/mathbits/compsci.../APgetline.htm
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
noobie is offline   Reply With Quote
Old 01-06-2007, 04:23 PM   #7 (permalink)
Forum Expert
 
Axle's Avatar
 
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
Default

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 is offline   Reply With Quote
Old 01-06-2007, 04:31 PM   #8 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

well, did you try my suggestion?

using cin.flush() before getline?
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
noobie is offline   Reply With Quote
Old 01-06-2007, 04:37 PM   #9 (permalink)
Forum Expert
 
Axle's Avatar
 
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
Default

Quote:
Originally Posted by noobie View Post
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

Last edited by Axle; 01-06-2007 at 05:05 PM.
Axle is offline   Reply With Quote
Old 01-06-2007, 04:46 PM   #10 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

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.
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
noobie is offline   Reply With Quote
Old 01-06-2007, 05:03 PM   #11 (permalink)
Forum Expert
 
Axle's Avatar
 
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
Default

Quote:
Originally Posted by noobie View Post
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);
}
Axle is offline   Reply With Quote
Old 01-06-2007, 05:17 PM   #12 (permalink)
Forum Expert
 
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
Default

hmzzz,

you could also try fflush(stdin);

anyway, I am glad I could help
__________________
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it."
noobie is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off



Powered by vBulletin® Version 3.7.0
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
SEO by vBSEO 3.2.0 RC5