-5

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;
 }
Freshu
  • 5
  • 3
  • The only thing I can recommend for this **homework** problem is that you should use `std::string` to manipulate text strings, not `char*` pointers. – alfC Feb 23 '23 at 19:49
  • char* is specified in the task text, so I can hardly change it :/ – Freshu Feb 23 '23 at 19:52
  • 1
    Sorry, you are taking a very bad C++ class then, run if you can. Also, homework questions are very badly regarded here in SO. Narrow your question if you can. – alfC Feb 23 '23 at 19:54
  • The assignment mixes the two; it's a bit of mess. There are lots of opportunities for improvements here. Using an actual class and taking better advantage of the Standard Library for starters. – sweenish Feb 23 '23 at 19:55
  • If the entered position is not considered valid you leak some memory in the loop. – Martin B. Feb 23 '23 at 19:56
  • 3
    @alfC Homework questions are not badly regarded here. Poor questions are. I don't have the link handy, but there is a guide for asking homework questions in an acceptable manner that helps those asking actually learn. – sweenish Feb 23 '23 at 19:57
  • `string first name,` is not valid, though here the task is already mentioning `string`. Also needing a `char*` is not reason to not use `std::string`. `std::string` does grant you access to the underlying array if needed – 463035818_is_not_an_ai Feb 23 '23 at 20:02
  • What is the meaning of "But it's not running well" ? If you need help with the code you need to be specific about input, output and expected output, it should be included in the question – 463035818_is_not_an_ai Feb 23 '23 at 20:04
  • It's not run at all. Visual studio keep crushing. As a result, I need to obtain the listed employees with their data specified in the programme. But usually during entering any of values visual keep shuting down the programme. – Freshu Feb 23 '23 at 20:10
  • if Visual studio crashes then that is a problem of visual studio. And then too, you would need to explain what steps exactly you take to make visual studio crash – 463035818_is_not_an_ai Feb 23 '23 at 20:21
  • 1
    You can add `#define _CRT_SECURE_NO_WARNINGS` as the first line of your program, before any includes, to suppress the `strcpy` warning. The warning message also tells you how to do that. – Retired Ninja Feb 23 '23 at 20:36

1 Answers1

1

I see one question in your post:

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

To bypass this (as Visual Studio suggests right next to that error)

error C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. To disable deprecation, use _CRT_SECURE_NO_WARNINGS. See online help for details.

, you can add this at the top of your file:

#define _CRT_SECURE_NO_WARNINGS

Alternatively, and to avoid memory leak in case of invalid user entry (as was pointed in the comments above), I would use strdup() to duplicate your string. Note that you would have to free() it instead of delete.

You do have a bigger problem that will cause an infinite loop if you enter non-number for the double salary. Your cin will go into a bad state and won't accept any more input.

To fix that, you need to use cin.ignore(); see When and why do I need to use cin.ignore() in C++? for esplanation.

Vlad Feinstein
  • 10,960
  • 1
  • 12
  • 27