| | |
Embarassingly simple buffer stream problem.
![]() |
•
•
Join Date: Dec 2009
Posts: 1
Reputation:
Solved Threads: 0
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.
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
C++ Syntax (Toggle Plain Text)
void EmployeeList::BuildEmployee (void) { int skill, benefit; string fstname, lstname; long SSN; float hours, perCont; cout << "Enter employee first name" << endl; getline (cin, fstname); E2.setFirstName(fstname); 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* cout << "Enter employee last name" << endl; getline (cin, lstname); E2.setLastName(lstname); cout << endl << fstname << " " << lstname << endl; // just to see if it ACTUALLY HAPPENED just within this function D:< cout << "Enter social security number" << endl; cin >> SSN; E2.setSSN(SSN); cout << "Enter skill level" << endl; cin >> skill; E2.setSkill (skill); cout << "Enter hours worked" << endl; cin >> hours; E2.setHours(hours); cout << "Enter benefit code" << endl; cin >> benefit; E2.setBenefit(benefit); if (skill == 4) { cout << "Enter retirement contribution percent" << endl; cin >> perCont; E2.setPercent(perCont); } }
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
•
•
Join Date: Nov 2009
Posts: 114
Reputation:
Solved Threads: 27
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
----------------------------------------------------------
http://clipboard.it - Social Pasting
http://twitter.com/programmersbook - Follow me
•
•
Join Date: Nov 2008
Posts: 409
Reputation:
Solved Threads: 77
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
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.
cout.flush().
Other things that you might have wrong are that the call
E2.setFirstName(fstname); might actually be to a method like thisvoid 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
•
•
Join Date: Oct 2009
Posts: 62
Reputation:
Solved Threads: 5
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.
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
C++ Syntax (Toggle Plain Text)
#include<iostream> using namespace std; class EmployeeList { public: EmployeeList(){} void BuildEmployee(void); string setFirstName(string); string setLastName(string); void setName(string,string); }; string EmployeeList::setFirstName(string FName) { return FName; } string EmployeeList::setLastName(string LName) { return LName; } void EmployeeList::setName(string FName,string LName) { cout<< setFirstName(FName)<<" "<<setLastName(LName)<<endl; } void EmployeeList::BuildEmployee (void) { int skill, benefit; string fstname, lstname; long SSN; float hours, perCont; cout << "Enter employee first name" << endl; getline (cin, fstname); setFirstName(fstname); cout << "Enter employee last name" << endl; getline (cin, lstname); setLastName(lstname); setName(fstname,lstname); //cout << endl << fstname << " " << lstname << endl; } int main() { EmployeeList var; var.BuildEmployee(); return 0; }
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
![]() |
Similar Threads
- buffer vs stream (C++)
- How do I flush the input stream? (C++)
- A problem, I'm stucked in OpenGl (C++)
- Input Stream Problem (C++)
- function changing order while executing program!!! (C++)
- IO problem... (C)
- overloaded input stream help (C++)
- Help with dates (C)
- Simple answer for vexing problem? (OS X)
- printf buffer strange behaviur (C)
Other Threads in the C++ Forum
- Previous Thread: while loop problems
- Next Thread: Ways to parse!!!!
Views: 336 | Replies: 3
| Thread Tools | Search this Thread |
Tag cloud for C++
6 algorithm array arrays assignment basic beginner binary c++ c++borland c/c++ calculator char class classes code compile compiler constructor conversion convert count delete dll dynamic encryption error file files form forms fstream function functions game givemetehcodez graph graphics gui homework iamthwee image input int lazy link linker list loop loops map math matrix member memory newbie number numbers object objects opengl output parameter pointer pointers problem program programming project python random read reading recursion recursive reference return search sort spoonfeeding string strings struct student studio template templates text time tree variable vc++ vector video visual win32 window windows winsock wxwidgets





