Member function not working with second object.

So I've been trying to learn OOP with C++ and this is my very first OOP type program. Supposed to create a class with 3 "attributes": first name, last name and salary.

Then I need to create two objects and collect those attributes from user input. Then I need to print those attributes to the screen.

I started small with just one attribute and one object. Moved up to one object with three attributes and everything was good to go.

With the second object however, the first member function (for the first name) isn't working correctly. The program doesn't accept user input. It's as if it just bypasses it.

I can't seem to trouble shoot this either. Not knowing why is driving me nuts.

Here's the code.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
//practice.h (header file)
#include <string>
using namespace std;


class Employee
{
public:
	Employee (string, string, int);
	
	void setFirstName(string);
	string getFirstName();
	void setLastName(string);
	string getLastName();
	void setSalary(int);
	int getSalary();

	void displayInfo();

private:
	string firstName;
	string lastName;
	int salary;
};

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
//interface.cpp (Implementation file)
#include "practice.h"
#include <iostream>
#include <string>
using namespace std;

Employee::Employee(string fname, string lname, int bankroll)
{
	setFirstName(fname);
	setLastName(lname);
	setSalary(bankroll);
}

void Employee::setFirstName(string name)
{
	cout << "What is the employee's first name? \n" << endl;
	getline (cin, name);
	firstName = name;
	cout << endl;
}

string Employee::getFirstName()
{
	return firstName;
}

void Employee::setLastName(string name)
{
	cout <<"What is the employee's last name? \n" << endl;
	getline (cin, name);
	lastName = name;
	cout << endl;
}

string Employee::getLastName()
{
	return lastName;
}

void Employee::setSalary(int cheddah)
{
	cout <<"What is the employee's annual salary? \n" << endl;
	cin >> cheddah;
	if (cheddah < 0)
		cheddah = 0;
	salary = cheddah;
	cout << endl;
}

int Employee::getSalary()
{
	return salary;
}

void Employee::displayInfo()
{
	cout << getFirstName() << "\t\t" << getLastName() << "\t\t" << getSalary() << endl;
	cout << endl;
}

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
//main.cpp (driver)
#include "practice.h"
#include <iostream>
#include <string>
using namespace std;

void main()
{
	Employee employeeOne("William", "Shakespear", 12000);
	Employee employeeTwo("Larry", "Tate", 18000);

	cout << "Here is what we have so far:\n" << endl;
	cout << "First Name" << "\tLast Name" << "\tSalary" << endl;
	cout << "==========" << "\t==========" << "\t==========" << endl;
	
	employeeOne.displayInfo();
	employeeTwo.displayInfo();
}

What is the employee's first name?

Marty

What is the employee's last name?

Mcfly

What is the employee's annual salary?

12000

What is the employee's first name?


What is the employee's last name?

George

What is the employee's annual salary?

14000

Here is what we have so far:

First Name      Last Name       Salary
==========      ==========      ==========
Marty           Mcfly           12000

                George          14000

Press any key to continue . . .


Is this a gross error in process or something? I can't seem to wrap my head around it.
The problem is most likely due to the fact that you are using getline inside the set-function. Getters and setters should be just that, so I would change the setFirstName to:
1
2
3
4
void Employee::setFirstName(string name)
{
	firstName = name;
}


And similarly for the other set-functions. The reason why your code is not working is that you're passing a string to the setFirstName-function, but then that string is being overwritten by getline, which gets no input.
Thanks for the response.

If I remove the user prompts from the set-functions, I can get both of the objects to display correctly... but ONLY if I initialize the three attributes of each object. While this is good because it helped me understand what was going on with the object, the constructor and the attributes, I would still like to be able to prompt the user for the first name, last name and salary of each object (employee).

I can do it structurally inside of main, but main starts to grow in size. For instance:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void main()
{
     string firstName;
     string lastName;
     int salary = 0;

//Prompt user to input the three variable values
     cout << "Employee001 first name: " << endl;
     getline (cin, firstName);
     cout << "Employee001 last name: " << endl;
     getline (cin, lastName);
     cout << "Employee001 annual salary: " << endl;
     cin >> salary;

//Create the first object and pass these values to the constructor
     Employee employeeOne(firstName, lastName, salary);
}


While this will work, I must create those user prompt lines (8 - 13) for each employee so that the values can be passed to the constructor with the creation of a new object. Main will start to get huge.

My goal is: When I create a new object, there are existing functions or code that will execute (regardless of object) that will prompt the user to input the first name, last name and salary so that when I want to display that information... it's a simple call to that object's attribute get member function.

Should I create three more member functions whose goal is to prompt the user for first name, last name and salary?

Then call these functions (returning the value) from the set function? Or would this be bad practice?
Update:

I tried to implement the above code into main but the program is still skipping over input the second time around (second object).

This is what I did:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void main ()
{
	string fName;
	string lName;
	int salary = 0;

	cout << "What is Employee001's first name?" << endl;
	getline (cin, fName);
	cout << "What is Employee001's last name?" << endl;
	getline (cin, lName);
	cout << "What is Employee001's annual salary?" << endl;
	cin >> salary;
	cout << endl;

	Employee employeeOne(fName, lName, salary);

	cout << "What is Employee002's first name?" << endl;
	getline (cin, fName);
	cout << "What is Employee002's last name?" << endl;
	getline (cin, lName);
	cout << "What is Employee002's annual salary?" << endl;
	cin >> salary;
	cout << endl;

	Employee employeeTwo(fName, lName, salary);
}


And this is the output screen:
What is Employee001's first name?
Freddy
What is Employee001's last name?
Kruger
What is Employee001's annual salary?
12000

What is Employee002's first name?
What is Employee002's last name?


Lines 17 & 18 input are being ignored. *sigh.... I need motrin*
It does the same thing even if I have two sets of variables.
Last edited on
The problem might be the mixture of >> and getline.

Try calling

cin.ignore();

after you read the salary.

It might be better to use getline for the salary as well, but you'll have to check and convert the input string to an int yourself.

Andy

Similar, older thread : "getline(cin,str) problem"
http://www.cplusplus.com/forum/beginner/7655/
Solved. I should have paid more attention to fafner's posting.

While I don't understand WHY the getline was causing issues, when I switched it to just plain old cin... the function works exactly as I want it to and prompts the user to input the first name, last name and salary for each object created.

Thank you fafner.

Topic archived. No new replies allowed.