Variable Conversions

Pages: 12
So I did a bit of searching only to find I couldn't make heads or tails of the parse stuff. I'm trying to convert string input for variables:one char, one int, and one double. I'm trying to convert inside a whole other function. If there is an easier way, please let me know. I could really use some assistance here! Thanks in advance.

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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Employee
{
private:
	string fname, lname, gender, dependents, anSal;

public:
	Employee()
	{
		fname = "Not Given";
		lname = "Not Given";
		gender = "U";
		dependents = "0";
		anSal = "20,000";
	}
	Employee(string a, string b, string c, string d, string e)
	{
		fname = fname;
		lname = lname;
		gender = gender;
		dependents = dependents;
		anSal = anSal;
	}
	void getInfo(string gender, string dependents, string anSal);//Input information for each employee
	void convert(string gender, string dependents, string anSal);
	void calcPay();//Calculates the salary 
	void prntInfo();//Prints the information of each employee
};

void displayDivider(int &empCount, int title)
{
	if (title == 1)
	{
		cout << "--------------------- Employee " << empCount << " ---------------------\n";
		return;
	}
	else if (title == 0)
	{
		cout << "-------------------------------------------------------\n";
	}
}//end divider
void convert(string gender, string dependents, string anSal)//it should convert in a function right?
{
	char x;
	int y;
	double z;
	x = gender[0];
	if ((x == 'F') || (x == 'f'))
	{
		x = 'F';
	}
	else if ((x == 'M') || (x == 'm'))
	{
		x = 'M';
	}
	static const string dependents("0123456789");

}
void getInfo(string fname, string lname, string gender, string dependents, string anSal)//begin information
{
	bool loop;
	char answer;
	int empCount = 0;
	cout << "Do you wish to add Employee information? Q to quit, Y to add.\t";
	cin >> answer;
	answer = toupper(answer);
	//Check
	while ((answer != 'Q') || (answer != 'Y'))//check for correct entries
	{
		cout << "\nError: Answer must be a character." << endl;
		cout << "Enter Y to add, Q to quit.\t";
		cin >> answer;
	}//end while loop
	
	if (answer == 'Q')
	{
		loop = false;
	}
	else if (answer == 'Y')
	{
		loop = true;
	}//End else if
	
	while (loop == true)
	{
		empCount++;
		int title;
		title = 1;
		displayDivider(empCount, title);
		
		//first name
		cout << "\nEnter First Name:\t";
		getline(cin, fname);
		
		//last name
		cout << "\nEnter Last Name:\t";
		getline(cin, lname);
		
		//gender
		cout << "\nEnter Gender (Male or Female):\t";
		getline(cin, gender);
		
		//Dependents
		cout << "\nEnter Dependents:\t";
		getline(cin, dependents);
		
		//Salary
		cout << "\nEnter Annual Salary:\t";
		getline(cin, anSal);

		cout << "\n" << endl;
		title = 0;
		displayDivider(empCount, title);
		//check to continue
		cout << "Would you like to add Employee Information? Y to add, Q to quit:\t";
		cin >> answer;
		answer = toupper(answer);
		switch (answer)
		{
		case 'Q': loop = false;
		case 'Y': loop = true;
		default: 
			while ((answer != 'Q') || (answer != 'Y'))//check for correct entries
			{
				cout << "\nError: Answer must be a character." << endl;
				cout << "Enter Y to add, Q to quit.\t";
				cin >> answer;
			}//end while loop
		}//end switch case for quit or continue
	}//end while loop
}//end get info
int main()
{
	
}
Last edited on
1
2
3
4
5
6
7
8
	Employee(string a, string b, string c, string d, string e)
	{
		fname = fname;
		lname = lname;
		gender = gender;
		dependents = dependents;
		anSal = anSal;
	}


What is the point of this? you pass in 5 strings only to set them equal to themselves and they are undefined.
Last edited on
Oh, right. That's temporary. We're required to use constructors and I haven't gotten to the part where I actually do something with it. Also, they need to be undefined because later in the function they will be input by the user.

The part I really need help with is the conversion. Unfortunately it is required and I have no idea how to work with conversions.
Last edited on
I'm unsure as to what this accomplishes at all, as it doesn't seem to do anything. However you are declaring another variable called dependents when you have already passed one in. This will be a problem.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void convert(string gender, string dependents, string anSal)//it should convert in a function right?
{
	char x;
	int y;
	double z;
	x = gender[0];
	if ((x == 'F') || (x == 'f'))
	{
		x = 'F';
	}
	else if ((x == 'M') || (x == 'm'))
	{
		x = 'M';
	}
	static const string dependents("0123456789");

}


EDIT: you have declared one in the class declaration as well so if you intend to use that one you wont need to pass it in as class members have direct access to it.
Last edited on
Also, they need to be undefined because later in the function they will be input by the user.
They don't need to be undefined you can just assign another value to them.
Last edited on
The part I really need help with is the conversion. Unfortunately it is required and I have no idea how to work with conversions.


explain how the conversion needs to happen and I'll mock an example up
Thanks for all your points, will make those changes in that case!

The string gender needs to be entered as a string by the user, and then changed somewhere into a character type.

The string dependents must be entered as a string by the user, and then changed into a integer type.

Lastly, the string anSal must be entered as a string by the user, and then changed into a double type.

Words from assignment:
Prompt for and then set the first name, last name, gender, dependents, and annual salary. (Remember that you have to convert gender, dependents, and annual salary from strings to the appropriate data type.)

That's really all I've been given for how they need to work.
Last edited on
I'm working on your class be patient and I'll post a revised edition with comments.
Alrighty, take your time. Thanks a lot for the help.
This is not a complete rework but it will get you started.

Apologies for taking so long, life has a habbit of getting in the way

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
#include <iostream>
#include <iomanip>
#include <string>

using namespace std;

class Employee
{
private:
	// firstly I changed the variables to their appropriate types
	// as it is preferable to not have 2 seperate varible to represent the same thing,
	// unless of course you need a string varible for each aswell.
	// Also a prefix of the type can help in distinguishing the varible type in code.
	string s_fname, s_lname;
	char c_gender;
	int i_dependents; 
	double d_anSal;

public:
	// Constructors
	Employee();
	Employee(string fname, string lname, string gender, string dependents, string anSal);

	// Member Functions
	char ConvertGender(string gender);
	int ConvertStringtoInt(string dependents);
	double ConvertStringtoDouble(string anSal);
	void print();

};

Employee::Employee() 
// <-- also note when providing a definition outside of the class delcaration
// you use classname::before the name and after the return type like so.

: // <-- An alternate way to initialize just added FYI.
// It is also the only way to initialize constant member values that I know of.
s_fname("Not Given"),
s_lname("Not Given"),
c_gender('U'),
i_dependents(0),
d_anSal(20000) // <-- do not put comma after the last member
{

}

Employee::Employee(string fname, string lname, string gender, string dependents, string anSal)
{
	s_fname = fname;
	s_lname = lname;
	c_gender = ConvertGender(gender);
	i_dependents = ConvertStringtoInt(dependents);
	d_anSal = ConvertStringtoDouble(anSal);
}

char Employee::ConvertGender(string gender)
{
	if (gender[0] == 'F' || gender[0] == 'f')
	{
		return 'F';
	}
	else if (gender[0] == 'M' || gender[0] == 'f')
	{
		return 'M';
	}
	else
	{
		cout << "Mutant baby Ahhhhh" << endl; // <-- A joke you should probably remove.
						  // Joke is on you if you hand this in without reading my comments :P
		return 'U';
	}
}

int Employee::ConvertStringtoInt(string dependents)
{
	int value = 0; // <-- a value to store the converted number

	for (int i = 0; i < dependents.length(); i++)
	{
		value *= 10;
		// we multiply value by 10 to move the digits one base 10 position
		value += dependents[i] % 48; 
		// the value of any ASCII number character ( 0 - 9)  
		// can be converted from ASCII format to an integer value
		// with % 48
		
	}

	return value;
}

double Employee::ConvertStringtoDouble(string anSal)
{
	return 0.0; // <-- I will leave this for you to figure out look at
				// ConvertStringToInt() and see if you can think of a way
				// to manipluate your sting to a double i will be interested to see your results
				// please post them once you have done it.
}

void Employee::print()
{
	cout << Employee::s_fname << endl 
		 << Employee::s_lname << endl 
		 << Employee::c_gender << endl 
		 << Employee::i_dependents << endl 
		 << Employee::d_anSal;
}

int main()
{
	Employee client("bug", "head", "male", "1932", "4332");
	client.print();
}
Last edited on
Thank you very much! The comments were informative and quite funny! I'll post the code once I have it finished! :)
You are welcome :P.

If you would like me to elaborate on this (below) I would be happy to.

// the value of any ASCII number character ( 0 - 9)
// can be converted from ASCII format to an integer value
// with % 48


And you should also be aware I have left error checking out of my conversion functions.

You should ask yourself what would happen if they entered an invalid input.

for instance if a string dependents = "5.2" // .2 for a very small child who doesn't eat a lot :P the output would become absurd. You will have to add a check for this.
Last edited on
Oh, yes, if you could elaborate on the Ascii conversion that would be great!

EDIT: Found some errors and solved them, but I can't seem to get passed the constructor to convert.

Code for double conversion
1
2
3
4
5
6
double Employee::ConvertStringtoDouble(string anS)
{
	double value = 0.0;
	value = atof(anS.c_str());
	return value;
}


Code adjustment for convert to integer
1
2
3
4
5
6
7
8
9
10
11
int Employee::ConvertStringtoInt(string dpnt, int x)
{
	int value = 0;
		value *= 10;
		// we multiply value by 10 to move the digits one base 10 position
		value += dpnt[x] % 48;
		//converts from ascii to integer value
		value = (round(value));//rounds value to nearest integer

	return value;
}


Entire 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>  
#include <stdexcept>
using namespace std;

//Class
class Employee
{
private:
	string fname[5], lname[5];
	string gender[5], dependents[5], anSal[5];
	char gen[5];
	int dep[5];
	int x;
	double anSalary[5], wkSal[5];
	int title, empCount;


public:
	// Constructors
	Employee()
	{
		fname[x] = "Not Given";
		lname[x] = "Not Given";
		gen[x] = 'U';
		dep[x] = 0;
		anSalary[x] = 20000;
	}
	Employee(string fname[], string lname[], string gender[], string dependents[], string anSal[])
	{
		getInfo();
		for (int x = 0; x <= empCount; x++)
		{
			fname[x] = fname[x];
			lname[x] = lname[x];
			string gndr = gender[x];
			string dpnt = dependents[x];
			string anS = anSal[x];
			gen[x] = ConvertGender(gndr);
			dep[x] = ConvertStringtoInt(dpnt, x);
			anSalary[x] = ConvertStringtoDouble(anS);
			calcWkSal();
			wkSal[x] = wkSal[x];
		}//end for
	}//end employee constructor

	// Member Functions

	//Info::Input
	void Employee::getInfo();//Input
	void Employee::calcWkSal();//Processing
	
	//Conversions::Processing
	char Employee::ConvertGender(string gndr);
	int Employee::ConvertStringtoInt(string dpnt, int x);
	double Employee::ConvertStringtoDouble(string anS);

	//Printing::Output
	void Employee::print();
	void Employee::displayDivider();
};

//Inputs & Calculations
void Employee::getInfo()//Good to go, errors gone!
{
	bool loop = true;
	int answer = 0;
	empCount = 0;
	cout << "Do you wish to add Employee information? 0 to quit, 1 to add.\t";
	cin >> answer;
	//Check
	if (answer == 0)
	{
		loop = false;
	}
	else if (answer == 1)
	{
		loop = true;
	}
	else
	{
		while ((answer != 1) || (answer != 0))//check for correct entries
		{
			cout << "\nError: Answer must be a number." << endl;
			cout << "Enter 1 to add, 0 to quit.\t";
			cin >> answer;
		}//end while loop
	}

	while (loop == true)
	{
		empCount++;
		if (empCount == 5)
		{
			cout << "\nError: Too many employees.\n";
			loop = false;
			return;
		}
		else
		{
			loop = true;
		}
		title = 1;
		displayDivider();

		//first name
		cout << "\nEnter First Name:\t";
		cin >> fname[empCount];

		//last name
		cout << "\nEnter Last Name:\t";
		cin >> lname[empCount];

		//gender
		cout << "\nEnter Gender:\t\t";
		cin >> gender[empCount];
		while ((gender[empCount] != ("female")) && (gender[empCount] != ("male")) && (gender[empCount] != ("Female")) && (gender[empCount] != ("Male")))
		{
			cout << "Error: Gender must be Male or Female.\n";
			cout << "Enter Gender:\t\t";
			cin >> gender[empCount];
		}
		//Dependents
		cout << "\nEnter Dependents:\t";
		cin >> dependents[empCount];

		//Salary
		cout << "\nEnter Annual Salary:\t";
		cin >> anSal[empCount];

		cout << "\n" << endl;
		title = 0;
		displayDivider();//end divider
		//check to continue
		cout << "Would you like to add Employee Information? 1 to add, 0 to quit:\t";
		cin >> answer;
		switch (answer)
		{
		case 0: loop = false; break;
		case 1: loop = true; break;
		default:
			while ((answer != 0) && (answer != 1))//check for correct entries
			{
				cout << "\nError: Answer must be a number." << endl;
				cout << "Enter 1 to add, 0 to quit.\t";
				cin >> answer;
			}//end while loop
		}//end switch case for quit or continue
	}//end while loop
}//end get info
void Employee::calcWkSal()
{
	for (int i = 0; i <= empCount; i++)//runs every employee through and stores the values
	{
		wkSal[i] = anSalary[i] / 52;
	}	//end for
}//weekly salary calculations

//Conversions
char Employee::ConvertGender(string gndr)
{
	if (gndr[0] == 'F' || gndr[0] == 'f')
	{
		return 'F';
	}
	else
	{
		return 'M';
	}

}
int Employee::ConvertStringtoInt(string dpnt, int x)
{
	int value = 0;
		value *= 10;
		// we multiply value by 10 to move the digits one base 10 position
		value += dpnt[x] % 48;
		//converts from ascii to integer value
		value = (round(value));//rounds value to nearest integer

	return value;
}
double Employee::ConvertStringtoDouble(string anS)
{
	double value = 0.0;
	value = atof(anS.c_str());
	return value;
}

//Output
void Employee::print()
{
	cout << "\n\nEmployee Information:\n";
	for (int i = 0; i <= empCount; i++)
	{
		title = 0;
		displayDivider();
		cout << "Name:\t" << fname[i] << " " << lname[i] << endl;
		cout << "Gender:\t" << gen[i] << endl;
		cout << "Dependents:\t" << dep[i] << endl;
		cout << "Annual Salary:\t" << anSalary[i] << endl;
		cout << "Weekly Salary:\t" << wkSal[i] << endl;
	}//end for
	title = 0;
	displayDivider();
}//end print
void Employee::displayDivider()
{
	if (title == 1)
	{
		cout << "\n--------------------- Employee " << empCount << " ---------------------\n";
		return;
	}
	else if (title == 0)
	{
		cout << "-----------------------------------------------------\n";
	}//end ifs
}//end dividers

int main(string fname[], string lname[], string gender[], string dependents[], string anSal[])
{
	Employee client(fname, lname, gender, dependents, anSal);
	client.print();
}
Last edited on
Oh, yes, if you could elaborate on the Ascii conversion that would be great!



To begin you need to understand a little bit about memory. First a char is a byte, a byte is made up of 8 bits. looking at a byte of memory in binary (which is what your computer understands) it may look like this. 0000 0000 or this 1010 1110 or any other combination of 1's and 0's.

An ASCII character '1' in a byte of memory looks like this 0011 0001. But here's the catch, a byte integer value of 49 looks like this 0011 0001, while a byte integer value of 1 looks like this 0000 0001. So if you were to just simply assign like this
1
2
char integer  = '1';
int number = integer;


you are just transferring the 0011 0001 value into an integer variable. Which when read as an integer it will be 49. Thus the %48 will return the remainder. The value from 0-9 in both formats follow the same pattern.

There are other ways you could do this conversion of course, like flipping bits directly with bitwise operators (and this is used a lot in APIs like windows). If you have come across "flags" this is what it is doing flipping bits to represent something which you can then call a check on. It is also very efficient process. If you would like some information on this I would go here to get started http://www.computerscienceforeveryone.com/Course_1/Unit_12/Lesson_6/
but I can't seem to get passed the constructor to convert.


What do you mean by this?
Last edited on
I'm really not sure why you did all this. Please comment. It looks as though you are trying to add multiple employees to the same class but you should instead assign that the class to a vector or array as not to limit yourself to only 5 people. The x variable is also undefined in the first constructor and empCount is undefined in the second.

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
class Employee
{
private:
	string fname[5], lname[5];
	string gender[5], dependents[5], anSal[5];
	char gen[5];
	int dep[5];
	int x;
	double anSalary[5], wkSal[5];
	int title, empCount;


public:
	// Constructors
	Employee()
	{
		fname[x] = "Not Given";
		lname[x] = "Not Given";
		gen[x] = 'U';
		dep[x] = 0;
		anSalary[x] = 20000;
	}
	Employee(string fname[], string lname[], string gender[], string dependents[], string anSal[])
	{
		getInfo();
		for (int x = 0; x <= empCount; x++)
		{
			fname[x] = fname[x];
			lname[x] = lname[x];
			string gndr = gender[x];
			string dpnt = dependents[x];
			string anS = anSal[x];
			gen[x] = ConvertGender(gndr);
			dep[x] = ConvertStringtoInt(dpnt, x);
			anSalary[x] = ConvertStringtoDouble(anS);
			calcWkSal();
			wkSal[x] = wkSal[x];
		}//end for
	}//end employee constructor 


I can see numerous problems with the code you've added here I suggest you create the class to only account for one employee and just make an array of employee objects. Adding of menus can be done in main and loop for every employee added to the array. A vector would be preferable to an array as it will be dynamically allocated and give you more freedom.
Last edited on
Sorry for delayed response, I've been cracking my head at this code. I realized the multiple arrays was too much and went ahead to change it to something else completely. I'm not sure if it will work the way I'd like, but here's to hoping. I'll post the code below after I answer your questions and comments.

Whoa! Thanks for the detailed explanation about memory and the link. I've added it to my favorites!

What I meant by:

but I can't seem to get passed the constructor to convert.


Was that after the input I could not get anywhere. I received an error message and that was that.

Current Error Messages
Error 4 error LNK2019: unresolved external symbol "public: void __thiscall Employee::displayDivider(int &,int &)" (?displayDivider@Employee@@QAEXAAH0@Z) referenced in function "public: void __thiscall Employee::display(void)" (?display@Employee@@QAEXXZ) c:\Users\Damir\documents\visual studio 2013\Projects\test_cis247\test_cis247\Source.obj test_cis247
Error 5 error LNK1120: 1 unresolved externals c:\users\damir\documents\visual studio 2013\Projects\test_cis247\Debug\test_cis247.exe 1 1 test_cis247


New 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>  
#include <fstream>
#include <stdexcept>
using namespace std;

//Declaration
const char FileName[] = "EmployeeRecords.txt";

//Class
class Employee
{
private:
	string lname;
	string fname;
	char gender;
	int dependents;
	double anSal, wkSal;
	//for divider:
	int title, i;

public:
	// Constructors
	Employee()
	{
		fname = "Not Given";
		lname = "Not Given";
		gender = 'U';
		dependents = 0;
		anSal = 20000;
	}
	Employee(string f, string l, char g, int d, double s)
	{

	}
	double Employee::calcPay();
	void Employee::display();
	string Employee::getFirst();
	string Employee::setFirst(string fname);
	string Employee::getLast();
	string Employee::setLast(string lname);
	char Employee::getGen();
	char Employee::setGen(string gen, char gender);
	int Employee::getDep();
	int Employee::setDep(int dependent, string dep);
	double Employee::getSal();
	double Employee::setSal(string an, double anSal);
	void displayDivider(int &title, int &i);
};
void displayDivider(int &title, int &i)
{
	if (title == 1)
	{
		cout << "\n--------------------- Employee " << i << " ---------------------\n";
		i++;
		return;
	}
	else if (title == 0)
	{
		cout << "------------------------------------------------------\n";
	}//end ifs
}//end dividers
//get information:
string Employee::getFirst()
{
	title = 1;
	displayDivider(title, i);
	//input
	cout << "\nEnter First Name:\t";
	cin >> fname;
	return fname;
}
string Employee::getLast()
{
	cout << "\nEnter Last Name:\t";
	cin >> lname;
	Employee::setLast(lname);
	return lname;
}
char Employee::getGen()
{
	string gen;
	cout << "\nEnter Gender:\t\t";
	cin >> gen;
	//check for errors:
	while ((gen != ("female")) && (gen != ("male")) && (gen != ("Female")) && (gen != ("Male")))
	{
		cout << "Error: Gender must be Male or Female.\n";
		cout << "Enter Gender:\t\t";
		cin >> gen;
	}
	Employee::setGen(gen, gender);

	return gender;
}
int Employee::getDep()
{
	string dep;
	cout << "\nEnter Dependents:\t";
	cin >> dep;
	Employee::setDep(dependents, dep);
	return dependents;
}
double Employee::getSal()
{
	string an;
	cout << "\nEnter Last Name:\t";
	cin >> an;
	Employee::setSal(an, anSal);
	return anSal;
	title = 0;
	displayDivider(title, i);
}

//set values
string Employee::setFirst(string fname)
{
	fname = fname;
	return fname;
}
string Employee::setLast(string lname)
{
	lname = lname;
	return lname;
}
char Employee::setGen(string gen, char gender)
{
	if (gen[0] == 'F' || gen[0] == 'f')
	{
		gender = 'F';
		return gender;
	}
	else
	{
		gender = 'M';
		return gender;
	}
}
int Employee::setDep(int dependents, string dep)
{
	int value = 0;
	for (int i = 0; i < dep.length(); i++)
	{
		value *= 10;
		// we multiply value by 10 to move the digits one base 10 position
		value += dep[i] % 48;
		//converts from ascii to integer value
		value = (round(value));//rounds value to nearest integer
	}
	dependents = value;
	return dependents;
}
double Employee::setSal(string an, double anSal)
{
	double value = 0.0;
	for (int i = 0; i < an.length(); i++)
	{
		value = atof(an.c_str());
	}
	anSal = value;
	return anSal;
}

//ouput
double Employee::calcPay()
{
	wkSal = anSal / 52;
	return wkSal;
}
void Employee::display()
{
	cout << "\n\nEmployee Information:\n";
	title = 0;
	displayDivider(title, i);
	cout << "Name:\t" << fname << " " << lname << endl;
	cout << "Gender:\t" << gender << endl;
	cout << "Dependents:\t" << dependents << endl;
	cout << "Annual Salary:\t" << anSal << endl;
	Employee::calcPay();
	cout << "Weekly Salary:\t" << wkSal << endl;
	displayDivider(title, i);
}

//main
int main(string fname, string lname, char gender, int dependents, double anSal, string dep)
{
	cout << "Assignment:\tLAB2\nDeveloper:\tSierra McGivney\nDate Written:\t05/16/2014\nPurpose:\tInput and output employee information.\n\n";
	Employee one("", "", 'U', 0, 20000);
	Employee two("Sierra", "McGivney", 'F', 0, 65000);
	one.getFirst();
	one.getLast();
	one.getGen();
	one.getDep();
	one.getSal();
	one.display();
	two.display();

	return 0;
} //end main 

Note: I know that the set fname and set lname are useless but they're required in the assignment and I wasn't too sure what to put inside of them.
Here is an example of what I tried to convey you could do instead. I used an array in case you are not familiar with vectors.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main()
{
	Employee employees[20];

	for (int i = 0; i < 20; i++)
	{
		// menu stuff and string storage varibles and error checking here

		// enter stored data into an object varible of class Employee
		Employee e_NewEmployee(string fn, string ln, string gen, string dep, string ans);

		// store object into array
		employees[i] = e_NewEmployee;

		
	}

	// when you need to access a particular employee do it like this, this could also be in a loop
	employees[0].printinfo(); // using the index of the array for the employee you want
}
Last edited on
1
2
3
4
5
string Employee::setFirst(string fname)
{
	fname = fname;
	return fname;
}


Use different variable names for incoming strings and your class variable names.

Secondly if you are not altering the string you don't need to return a value or even do it in a function but saying you need to have a function it could look like this

1
2
3
4
5
6
7
8
9
10
contructor(string variable1, string namevariable)
{
	somevariable = blahblahfunction(variable1);
	setnamefunction(namevariable);
}

void setnamefunction(string namevariable)
{
	firstname = namevariable; // <-- you can access your member varibles directly from any member function
}


or even this if you prefer

1
2
3
4
string Employee::setFirst(string fname)
{
	return fname;
}
Last edited on
Pages: 12