I have one task to do:
The following structure is given:
struct employee { string first name, char* position; double salary; char gender;};
Write a function that creates and populates an array of n employees. The number of employees is only given after running the program. Remember to check the validity of the data: the fields first name and position consist of letters only; the salary field is a value greater than zero; the gender field takes only two values, 'K' or 'M'.
Load the individual values until the user enters the correct data.
Pass the value of n and a pointer to the array as function parameters.
Write a programme to test the function.
I wrote the code below. But I can't run it at all. Got errors: 1st of all I used function "strcpy" and visual studio point this as an error and indicate to change it to strcpy_s which I am not sure how to use (?). Could you look at my code and indicate what to do better/change so it would work?
EDIT: As a result, I need to obtain the listed employees with their data specified in the programme.
#include <iostream>
#include <string>
using namespace std;
struct employee {
string first_name;
char* position;
double salary;
char gender;
};
void createEmployees(int n, employee* &employees) {
employees = new employee[n]; // allocate memory for the array of employees
for (int i = 0; i < n; i++) {
cout << "Employee " << i + 1 << ":" << endl;
// input first name
string first_name;
while (true) {
cout << "First name: ";
cin >> first_name;
bool valid = true;
for (char c : first_name) {
if (!isalpha(c)) {
valid = false;
break;
}
}
if (valid) {
employees[i].first_name = first_name;
break;
} else {
cout << "Invalid input. Please enter only letters." << endl;
}
}
// input position
string position;
while (true) {
cout << "Position: ";
cin >> position;
employees[i].position = new char[position.length() + 1];
bool valid = true;
for (char c : position) {
if (!isalpha(c)) {
valid = false;
break;
}
}
if (valid) {
strcpy(employees[i].position, position.c_str());
break;
} else {
cout << "Invalid input. Please enter only letters." << endl;
}
}
// input salary
double salary;
while (true) {
cout << "Salary: ";
cin >> salary;
if (salary > 0) {
employees[i].salary = salary;
break;
} else {
cout << "Invalid input. Please enter a value greater than zero." << endl;
}
}
// input gender
char gender;
while (true) {
cout << "Gender (M/F): ";
cin >> gender;
if (gender == 'M' || gender == 'K') {
employees[i].gender = gender;
break;
} else {
cout << "Invalid input. Please enter 'M' or 'K'." << endl;
}
}
}
}
int main() {
int n;
cout << "How many employees? ";
cin >> n;
employee* employees;
createEmployees(n, employees);
// print employee details
for (int i = 0; i < n; i++) {
cout << "Employee " << i + 1 << ":" << endl;
cout << "First name: " << employees[i].first_name << endl;
cout << "Position: " << employees[i].position << endl;
cout << "Salary: " << employees[i].salary << endl;
cout << "Gender: " << employees[i].gender << endl;
cout << endl;
}
// deallocate memory for the array of employees
for (int i = 0; i < n; i++) {
delete[] employees[i].position;
}
delete[] employees;
return 0;
}