Employee and ProductionWorker Classes Assignment

I do not see my error here, would someone please be kind enough point it out to me and explain what I am doing wrong and how I fix it?

Thanks so much!

I am getting the following errors..

Error 1 error C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. Line 31

Error 2 error C2679: binary '<<' : no operator found which takes a right-hand operand of type 'std::string' (or there is no acceptable conversion) Line 84

Error 3 IntelliSense: no operator "<<" matches these operands
operand types are: std::basic_ostream<char, std::char_traits<char>> << std::string Line 84


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
#include <iostream>
#include <iomanip>
#include <cstring>
#include <cstdlib>
#include <cctype>
#include <fstream>
using namespace std;

class Employee
{
private:
	char *name;
	int empNo;
	char date[11];
public:
	Employee(){}
	~Employee(){}
	void printEmp()
	{
		cout << "   Employee's Name      : " << name;
		cout << "\n   Employee's Number    : " << empNo;
		cout << "\n   Employee's Hire Date : " << date;
	}
	void setName()
	{
		char temp[50];
		cout << "   Please enter employee name: ";
		cin.getline(temp, 99);
		int size = strlen(temp);
		name = new char[size];
		strncpy(name, temp, strlen(temp));
		name[size] = '\0';
	}

	void setEmpNo()
	{
		int temp;
		bool flag;
		cout << "   Please enter employee number in format XXXXX: ";
		cin >> temp;
		empNo = temp;
	}

	void setDate()
	{
		char temp[11];
		bool flag;
		cout << "   Please enter the date you were hired in format mm/dd/yyyy: ";
		do
		{
			flag = true;
			cin.getline(temp, 11);
			for (int i = 0; i < 11; i++)
			{
				if (i == 2 || i == 5)
				{
					i++;
					break;
				}
				if (!isdigit(temp[i]))
					flag = false;
			}
			if (strlen(temp) != 10)
				flag = false;
			int i = 0;
		} while (flag == false);
		for (int i = 0; i < 12; i++)
		{
			date[i] = temp[i];
		}
	}
};

class ProductionWorker : public Employee
{
private:
	double hourlyPay;
	string shift;
public:
	ProductionWorker() {}
	~ProductionWorker(){}
	void printPay()
	{
		cout << "\n   Employee's Shift  : " << shift;
		cout << "\n   Employee's Hourly Pay  : $" << hourlyPay;
	}
	void setShift()
	{
		int temp;
		do
		{
			cout << "   Please enter your shift, 1 for day and 2 for night: ";
			cin >> temp;
		} while (temp != 1 && temp != 2);
		if (temp == 1)
		{
			shift = " day";
		}
		else
		{
			shift = " night";
		}
	}

	void setPay()
	{
		double temp = 0;
		do
		{
			cout << "   Please enter your hourly pay: ";
			cin >> temp;
			if (hourlyPay < 0)
			{
				cout << "   Invalid data, try again";
			}
		} while (temp < 0);
		hourlyPay = temp;
	}


};


void Clear_Screen(void);
void Flush(void);

int main()
{
	ProductionWorker person;
	person.setName();
	person.setEmpNo();
	person.setDate();
	person.setPay();
	person.setShift();
	Clear_Screen();
	person.printEmp();
	person.printPay();

	cout << "\n\n   Thank you for using the program!" << endl;
	Flush();
	cout << "\n\n   Press any key to terminate . . ." << endl;
	getchar();
	return 0;
}


void Clear_Screen(void)
{
#ifdef _WIN32
	system("cls");
#else
	system("clear");
#endif
}


void Flush(void)
{
	int ch;
	do
	{
		ch = getchar();
	} while (ch != EOF && ch != '\n');
	clearerr(stdin);
}
You must be using Visual Studio. I use it, and it yells at me from time to time.

Microsoft deprecated strncpy (it is a source of buffer overflow) and replaced it with strncpy_s.
You can either change to strncpy_s or do something like this
1
2
std::string string1;
std::string2(string1);


also, use #include <string>
I added the #include <string> and it seemed to fix my line 84 issue. Now I am getting an error on line 33 (which is line 31 above) that states..

Error 1 error C4996: 'strncpy': This function or variable may be unsafe. Consider using strncpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details. Line 33
ok, I see what you were saying there, so I changed line 33 (31) to strncpy_s and it thew this error..

2 IntelliSense: no instance of overloaded function "strncpy_s" matches the argument list
argument types are: (char *, char [50], size_t) Line 33
Ah, ok. strncpy_s takes 4 arguments instead of 3.
You can call it like this:
strncpy(destinationString, destinationStringSize, sourceString, sourceStringSize);
destinationString is the new string, and destinationStringSize is the size of the new string.
sourceString is the original string (the one to be copied), and sourceStringSize is the size of the original string.

I really hate it when they deprecate functions. Strncpy is deprecated by Microsoft, but not by ISO.
ok, got it. Line 33 now looks likes this..

strncpy_s(name, strlen(name), temp, strlen(temp));

It compiles and it runs. I get a popup error that I can ignore, but I dont care as long as it works.

Thanks so much JayZom!
Be careful and read what the popup error says. They are useful for seeing something that could cause an issue.

Glad it worked.
Topic archived. No new replies allowed.