Embarassingly simple buffer stream problem.

Reply

Join Date: Dec 2009
Posts: 1
Reputation: Amawri is an unknown quantity at this point 
Solved Threads: 0
Amawri Amawri is offline Offline
Newbie Poster

Embarassingly simple buffer stream problem.

 
0
  #1
Dec 5th, 2009
Hi everyone. I'm a reta-I mean a beginner and 98% of my bugs are something really obvious... but I've checked this so much and I don't know what to do. This is my function to create an Employee object, called when the user wants to add a new employee to the system.

  1.  
  2. void EmployeeList::BuildEmployee (void)
  3.  
  4. {
  5. int skill, benefit;
  6. string fstname, lstname;
  7. long SSN;
  8. float hours, perCont;
  9.  
  10.  
  11. cout << "Enter employee first name" << endl;
  12. getline (cin, fstname);
  13. E2.setFirstName(fstname);
  14.  
  15. cin.ignore(10, '\n'); // somehow this line allows the next cout to print. I just found it in a sample program and it worked so I didn't question... *hides in shame*
  16.  
  17. cout << "Enter employee last name" << endl;
  18.  
  19. getline (cin, lstname);
  20.  
  21. E2.setLastName(lstname);
  22.  
  23.  
  24. cout << endl << fstname << " " << lstname << endl; // just to see if it ACTUALLY HAPPENED just within this function D:<
  25.  
  26. cout << "Enter social security number" << endl;
  27. cin >> SSN;
  28. E2.setSSN(SSN);
  29.  
  30. cout << "Enter skill level" << endl;
  31. cin >> skill;
  32. E2.setSkill (skill);
  33.  
  34. cout << "Enter hours worked" << endl;
  35. cin >> hours;
  36. E2.setHours(hours);
  37.  
  38. cout << "Enter benefit code" << endl;
  39. cin >> benefit;
  40. E2.setBenefit(benefit);
  41.  
  42. if (skill == 4)
  43. {
  44. cout << "Enter retirement contribution percent" << endl;
  45. cin >> perCont;
  46. E2.setPercent(perCont);
  47. }
  48.  
  49.  
  50. }

So... say I want to hire Franziska Von Karma.

What prints is

Enter employee first name
--> Franziska
Enter employee last name
--> Von Karma

Von Karma
Enter social security number
... etc

So, the first name 'Franziska' is just ... completely NOT read. I know it's not the setters. And... the last name and the rest of it works fine. o_o
Reply With Quote Quick reply to this message  
Join Date: Nov 2009
Posts: 114
Reputation: programmersbook is an unknown quantity at this point 
Solved Threads: 27
programmersbook programmersbook is offline Offline
Junior Poster
 
0
  #2
Dec 5th, 2009
Does the function "setFirstName" take the parameter as reference?
Everything what's guinness is simple but not everthing what's simple is guinness.
----------------------------------------------------------
http://clipboard.it - Social Pasting
http://twitter.com/programmersbook - Follow me
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 409
Reputation: StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light StuXYZ is a glorious beacon of light 
Solved Threads: 77
StuXYZ StuXYZ is offline Offline
Posting Pro in Training
 
0
  #3
Dec 5th, 2009
Ok the ugly cin.ignore is simply unnecessary. I have no idea why you might need it, it ignores the next 10 characters of input (say 10 random letters upto a return. Very strange. The std::endl should flush cout, so it should work. If you are using an old compiler you can add
cout.flush().

Other things that you might have wrong are that the call
E2.setFirstName(fstname); might actually be to a method like this
void setFirstName(std::string&); and then it changes the variable fstname.

I would also like to see some initialization of E2, and addition to the main list of employies. [along with some checking].

hope this helps but without some more code I can't help much more.
experience is the most expensive way to learn anything
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 62
Reputation: UberJoker is an unknown quantity at this point 
Solved Threads: 5
UberJoker UberJoker is offline Offline
Junior Poster in Training
 
0
  #4
Dec 5th, 2009
I am not sure what you mean by its not being read. I think it is being read thats why its printing it out. Just for making sure it is infact reading it, Here is a little name function I added. The name function would never output the names if they weren't being read.
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. class EmployeeList
  6. {
  7. public:
  8. EmployeeList(){}
  9. void BuildEmployee(void);
  10. string setFirstName(string);
  11. string setLastName(string);
  12. void setName(string,string);
  13. };
  14.  
  15. string EmployeeList::setFirstName(string FName)
  16. {
  17. return FName;
  18. }
  19.  
  20.  
  21. string EmployeeList::setLastName(string LName)
  22. {
  23.  
  24. return LName;
  25. }
  26.  
  27.  
  28.  
  29. void EmployeeList::setName(string FName,string LName)
  30. {
  31.  
  32. cout<< setFirstName(FName)<<" "<<setLastName(LName)<<endl;
  33. }
  34.  
  35.  
  36. void EmployeeList::BuildEmployee (void)
  37. {
  38.  
  39. int skill, benefit;
  40. string fstname, lstname;
  41. long SSN;
  42. float hours, perCont;
  43.  
  44.  
  45. cout << "Enter employee first name" << endl;
  46. getline (cin, fstname);
  47. setFirstName(fstname);
  48.  
  49.  
  50.  
  51. cout << "Enter employee last name" << endl;
  52. getline (cin, lstname);
  53. setLastName(lstname);
  54.  
  55.  
  56. setName(fstname,lstname);
  57. //cout << endl << fstname << " " << lstname << endl;
  58.  
  59.  
  60.  
  61. }
  62.  
  63. int main()
  64. {
  65. EmployeeList var;
  66. var.BuildEmployee();
  67. return 0;
  68. }

Sorry actually I didnt quite understand your question. But hope this helps.

Output:
[swj@localhost Problems]$ g++ test3.cpp
[swj@localhost Problems]$ ./a.out
Enter employee first name
Swaraj
Enter employee last name
Patil
Swaraj Patil
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 336 | Replies: 3
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC