class employee, variable problem

im trying to make a program that prints out the yearly salary of two employees and first prints the salary by multiplying the input monthly salary with 12 and then display it again but this time with an increment of 10%. if i use global variable the program over writes the values of employee 1 with employee 2 and displays the same result. but when i initialize them in the class then they are undefined in the main().

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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
 #include <iostream>
#include <string>
using namespace std;


class Employee
{
public:
	string firstname; //this is initialized in the class.
	string lastname;
	int salary;
	double total_salary;

	Employee(string firstname, string lastname, int salary)
	{
		void setfirst_name(string fname_input);
		void setlast_name(string lname_input);
		void setmonthly_salary(int salary_input);

	}
	void setfirst_name(string fname_input)
	{
		cout << "Enter First Name: ";
		cin >> fname_input;
		cout << "\n" << endl;
		firstname = fname_input;
	}
	string getfirst_name()
	{
		return firstname;
	}

	void setlast_name(string lname_input)
	{
		cout << "Enter Last Name: ";
		cin >> lname_input;
		cout << "\n" << endl;
		lastname = lname_input;
	}
	string getlast_name()
	{
		return lastname;
	}

	void setmonthly_salary(int salary_input)
	{
		cout << "Enter Monthly Salary: ";
		cin >> salary_input;
		cout << "\n" << endl;
		if (salary_input < 0)
		{
			cout << "Invalid Salary Amount. Salary Set To 0." << endl;
			salary_input = 0;
		}
		salary = salary_input;
	}

	int getmonthly_salary()
	{
		return salary;
	}
};


int main()
{
	int number;
	Employee employee1("a", "a", 0);
	Employee employee2("a", "a", 0);
	cout << "Enter Employee Number (1 or 2): ";
	cin >> number;
	system("CLS");

	if (number == 1)
	{
		employee1.setfirst_name(firstname);// error comes in these lines that first name etc is undefined
		employee1.setlast_name(lastname);
		employee1.setmonthly_salary(salary);

		system("PAUSE");
		system("CLS");
		cout << "First Name: " << employee1.getfirst_name() << "\n" << endl;
		cout << "Last Name: " << employee1.getlast_name() << "\n" << endl;
		cout << "Monthly Salary: " << employee1.getmonthly_salary() << "\n" << endl;
		cout << "The Yearly Salary of " << employee1.getfirst_name()
			<< " " << employee1.getlast_name()
			<< " with Monthly Salary of " << employee1.getmonthly_salary()
			<< " is " << employee1.getmonthly_salary() * 12 << endl;
	}

	if (number == 2)
	{
		employee2.setfirst_name(firstname); // same undefined error here
		employee2.setlast_name(lastname);
		employee2.setmonthly_salary(salary);
		
		system("PAUSE");
		system("CLS");
		cout << "First Name: " << employee2.getfirst_name() << "\n" << endl;
		cout << "Last Name: " << employee2.getlast_name() << "\n" << endl;
		cout << "Monthly Salary: " << employee2.getmonthly_salary() << "\n" << endl;
		cout << "The Yearly Salary of " << employee2.getfirst_name()
			<< " " << employee2.getlast_name()
			<< " with Monthly Salary of " << employee2.getmonthly_salary()
			<< " is " << employee2.getmonthly_salary() * 12 << endl;
	}

	if (number != 1 && number != 2)
	{
		return main();
	}

	system("pause");
	system("CLS");
	string cont;
	cout << "Do You Want To Enter Again? Press Y" << endl;
	cin >> cont;
	if (cont == "Y" || cont == "y")
	{
		return main();
	}

		cout << "Employee 1\n" << endl;
		cout << "First Name: " << employee1.getfirst_name() << "\n" << endl;
		cout << "Last Name: " << employee1.getlast_name() << "\n" << endl;
		cout << "Monthly Salary: " << employee1.getmonthly_salary() << "\n" << endl;
		employee1.getmonthly_salary()*1.1;
		cout << "The Yearly Salary of " << employee1.getfirst_name()
			<< " " << employee1.getlast_name()
			<< " with Monthly Salary of " << employee1.getmonthly_salary()
			<< " is " << (employee1.getmonthly_salary() * 12)*1.1 << endl;


		cout << "Employee 2\n" << endl;
		cout << "First Name: " << employee2.getfirst_name() << "\n" << endl;
		cout << "Last Name: " << employee2.getlast_name() << "\n" << endl;
		cout << "Monthly Salary: " << employee2.getmonthly_salary() << "\n" << endl;
		((int)(employee2.getmonthly_salary()*1.1));
		cout << "The Yearly Salary of " << employee2.getfirst_name()
			<< " " << employee2.getlast_name()
			<< " with Monthly Salary of " << employee2.getmonthly_salary()
			<< " is " << (employee2.getmonthly_salary() * 12)*1.1 << endl; // if i initialize the variables globally the data here gets overwritten.

	system("pause");
	return 0;
}

You want to ask the user for the name and THEN assign this string to your member variable I guess?

Also.. line 110 & 120.. Do NOT ever return main() like this. put the whole thing in a loop.
well the return main() thing ill fix it up. it occurred to me after a while what if the user enter a different number..... and yes.... that is exactly what i want to do
all good then :)
Topic archived. No new replies allowed.