Constructor not working?

I'm not sure what's going on, but whenever I try to run the program it stops after I quit the employee information entry and gives me a message telling me the program was crashed. Any ideas what I'm doing wrong?

Thanks in advance for help!

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
227
228
229
230
231
232
233
234
 #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];
			gen[x] = ConvertGender(gndr);
			dep[x] = ConvertStringtoInt(dpnt);
			anSalary[x] = ConvertStringtoDouble(anSal[x]);
		}//end for
		calcWkSal();
		wkSal[x] = wkSal[x];
	}//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);
	double Employee::ConvertStringtoDouble(string anSal);

	//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; return;
		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
	return;
}//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
}//end 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 value = 0;
	for (int i = 0; i < dpnt.length(); i++)
	{
		value *= 10;
		// we multiply value by 10 to move the digits one base 10 position
		value += dpnt[i] % 48;
		//converts from ascii to integer value
		value = (round(value));//rounds value to nearest integer
	}
	return value;
}
double Employee::ConvertStringtoDouble(string anSal)
{
	double value = 0.0;
	for (int i = 0; i < anSal.length(); i++)
	{
		value = atof(anSal.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();
}


Normally main is defined as
 
int main() { /* ... */ }
or
 
int main(int argc, char* argv[]) { /* ... */ }


But in your code main has five string pointer parameters. Are you sure your compiler can handle such a definition of main? Probably not...
Last edited on
Sorry, I don't understand. Why wouldn't it be able to handle this?

Also, it runs fine up until after the getInfo part, which pulls into the constructor. Would that not be affected by the inability to handle five parameters?
main is a special function -- there are restrictions on how it can be defined, so you can't just define it however you like.
The C++ standard requires that compilers be able to handle these two definitions of main:
int main()
or
int main(int argc, char* argv[]) (obviously, the names could be different or char* argv[] could be written as char** argv -- same thing).

The compiler isn't guaranteed to accept anything besides those two.
(Though I think some compilers will accept int main(int argc, char* argv[], char* envp[]))

If you want to be able to pass command-line arguments to your program, use the second definition. Then argc will be the number of arguments (including the name of the program) and argv will be an array of char* strings containing the arguments themselves.
For instance:
1
2
3
4
5
6
7
int main(int argc, char* argv[])
{
    std::cout << "The name of this program is: " << argv[0];
    std::cout << "\nAdditional arguments (if any) passed to this program:\n";
    for (int i = 1; i < argc; ++i)
        std::cout << argv[i] << '\n';
}
http://coliru.stacked-crooked.com/a/3096ec996d8d0483

Also, I don't get why you're using std::string arrays.
You want an employee to have up to 5 first names?
Multiple Employees is the reason behind five names and everything else. The name of the class is Employee, dictated by the homework assignment.

Edit: Also, thank you for that explanation!

Assignment Instructions:

Using the provided Class Diagram from Step 1, code the Employee class in the new project (i.e., "Realize the UML Class diagrams").
The default constructor should set the attributes as follows: firstName = "not given", lastName = "not given", gender = "U" (for unknown), dependents = 0, and annualSalary = 20,000.
The multi-arg constructor should initialize all of the attributes using values passed in using its parameter list.
As shown in the Class diagram, each attribute should have a "getter" to retrieve the stored attribute value, and a "setter" that modifies the value.
The calculatePay( ) method of the Employee class should return the value of annual salary divided by 52 (return annualSalary / 52;).
The displayEmployee() method should display all the attributes of the Employee object in a well-formatted string with logical labels applied to each attribute. Don't forget to call calculatePay from within the displayEmployee method in order to display the Employee's weekly pay as well!

In the Main class, create code statements that perform the following operations. Be sure you follow proper commenting and programming styles (header, indentation, line spacing, etc.).

Create an Employee object using the default constructor.
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.)
Using your code from last week, display a divider that contains the string "Employee Information"
Display the Employee information.
Create a second Employee object using the multi-argument constructor, setting each of the attributes with appropriate valid values.
Using your code from last week, display a divider that contains the string "Employee Information".
Display the Employee information for the second Employee object.
Last edited on
Since your class is called Employee (singular), I would have it be just one employee, and then possibly use an array of Employees if you want more than one (though it seems that you only need two for now, so an array probably won't be necessary -- you can just declare two different Employee objects).
Oh man. Thank you so much for that last sentence. I just clicked with simplicity. lol.

I appreciate your help!
Topic archived. No new replies allowed.