|
||
|
|
#1 (permalink) |
|
Forum Expert
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
|
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()
{
}
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);
}
Last edited by Axle; 01-06-2007 at 03:36 PM. |
|
|
|
|
|
#2 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
__________________
"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. |
|
|
|
|
|
#4 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
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." |
|
|
|
|
|
#6 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
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." |
|
|
|
|
|
#8 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
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." |
|
|
|
|
|
#9 (permalink) | |
|
Forum Expert
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
|
Quote:
Note: The program was written and compiled using VS 2005 SP1 Last edited by Axle; 01-06-2007 at 05:05 PM. |
|
|
|
|
|
|
#10 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
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." |
|
|
|
|
|
#11 (permalink) | |
|
Forum Expert
Join Date: Oct 2002
Location: Iowa, USA
Age: 38
Posts: 395
|
Quote:
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);
}
|
|
|
|
|
|
|
#12 (permalink) |
|
Forum Expert
Join Date: Jul 2005
Location: Istanbul/Turkey
Age: 27
Posts: 425
|
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." |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|