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.

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

Dani AI

Generated

Short summary and a focused way to pin this down.

’s symptom — the fstname read with getline appears empty later while the last name is fine — most often means the local string is being changed after it’s read, or it was never populated at the moment it’s inspected. , and have useful leads; the next steps below isolate which one it is and give a robust input pattern.

First, isolate the read vs the setter. Immediately after the getline(cin, fstname); add a diagnostic print (with delimiters) before calling any setter to prove the read succeeded. Then temporarily comment out the E2.setFirstName(...) call (or pass a deliberate copy: E2.setFirstName(std::string(fstname));) and re-run. If fstname is correct before the setter but empty after, the setter is modifying the argument. Inspect the declaration of setFirstName — a non-const reference parameter (std::string&) or a buggy implementation that calls clear() will mutate the caller’s variable. The safe signature is typically void setFirstName(const std::string& name) (or pass by value and move).

Second, make input handling consistent to avoid mixing line- and token-based reads later. Prefer reading full lines and parsing numbers from them; this avoids surprises when operator>> and getline are combined. Example patterns:

// setter
void Employee::setFirstName(const std::string& fn) { firstName = fn; }

// parse a number from a line
std::string line;
std::getline(std::cin, line);
std::istringstream iss(line);
long ssn = 0;
if (!(iss >> ssn)) { /* handle parse error */ }

Finally, enable diagnostics: compile with warnings (-Wall -Wextra), run under AddressSanitizer/UBSan or a debugger, and add a quick print inside setFirstName to see whether it mutates its parameter. The cin.ignore(10, '\n') seen in the code is hiding the real issue; it’s better to remove it once the root cause is fixed.

Recommended Answers

All 3 Replies

Does the function "setFirstName" take the parameter as reference?

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.

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.

#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

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.