Home > OS >  Passing a struct array and updating array in C
Passing a struct array and updating array in C

Time:12-08

For one of my options, I had to add a row of data to my struct array, I was able to update the array but it does not update the actual array. So I am trying to update my struct array by passing it as a reference. I placed the '&' sign before the name of the variable, but all my indexes within the function errored.. I'm still a newbie to programing and I'm not "allowed " to use any vectors according to my Professor. . I looked online and placed the '&' on the function name as well as on the prototype. I also changed it from void to the "data type" to return the updated array, but no luck... I hope this comes out clear....

#include<iostream>
#include<iomanip>
#include<fstream>

using namespace std;

struct Data {
    string first_name;
    string last_name;
    int employee_id;
    string phone_number;
};



void readFile( ifstream& , string, Data [],int&);
void getEmployeeDetail(Data [], int&);
void getEmployeeLast(Data [], int&);
Data getAddEmployee(Data &, int&);
void getRemoveEmployee();
void getManagerOnly();
void getStaffOnly();

const int SIZE = 15;
Data employee[SIZE];

int main() {

    ifstream inputFile;
    ofstream outputFile;
    int count = 0;
    int selection;
    const int EMPLOYEE_DETAIL = 1,
              EMPLOYEE_LAST = 2,
              ADD_EMPLOYEE = 3,
              REMOVE_EMPLOYEE = 4,
              MANAGER_ONLY = 5,
              STAFF_ONLY = 6,
              QUIT = 7;

    readFile( inputFile ,"employeeData.txt", employee,count);


    do {
        
        cout << "\t Please select your option.\n"
            << "1. List all employee details\n"
            << "2. List employee by last name\n"
            << "3. Add a new employee\n"
            << "4. Remove an employee\n"
            << "5. Show all managers only\n"
            << "6. Show all staff only\n"
            << "7. Quit\n"
            << "Please enter your selection.\n" << endl;
        cin >> selection;

        while (selection < EMPLOYEE_DETAIL || selection > QUIT) {
            cout << " Pleasse enter a valid selection.\n";
                cin >> selection;
            }


        switch (selection) {
        case EMPLOYEE_DETAIL:
            getEmployeeDetail(employee,count);
            break;

        case EMPLOYEE_LAST:
            getEmployeeLast(employee, count);
            break;

        case ADD_EMPLOYEE:
            getAddEmployee(employee, count);
            break;

        case REMOVE_EMPLOYEE:
            getRemoveEmployee();
            break;

        case MANAGER_ONLY:
            getManagerOnly();
            break;

        case STAFF_ONLY:
            getStaffOnly();
            break;

        case QUIT:
            cout << "Program ending\n";
            break;
        }

    } while (selection != QUIT);

    system("pause");
    return(0);
}


void readFile(ifstream& inputFile, string data, Data employee[],int &count) {

    

    inputFile.open(data);
    if (!inputFile) {
        cout << " Error in opening file\n";
        exit(1);
    }
    else {

        while (!inputFile.eof()) {
            inputFile >> employee[count].first_name;
            inputFile >> employee[count].last_name >> employee[count].employee_id >> employee[count].phone_number;
            count  ;
        }


        inputFile.close();
    }

    return;
};
void readFile(ifstream& inputFile, string data, Data employee[],int &count) {

    

    inputFile.open(data);
    if (!inputFile) {
        cout << " Error in opening file\n";
        exit(1);
    }
    else {

        while (!inputFile.eof()) {
            inputFile >> employee[count].first_name;
            inputFile >> employee[count].last_name >> employee[count].employee_id >> employee[count].phone_number;
            count  ;
        }

        inputFile.close();
    }

    return;
};
void getEmployeeDetail( Data employee[], int& count) {
    
    cout << "Firt Name" << "\t" << "Last Name " << "\t" << "Employee ID " << "\t" << "Phone Number \n";
    cout << "--------------------------------------------------------------------------\n";
    for(int i =0; i < count; i  )
    cout <<employee[i].first_name<<"\t\t"<<employee[i].last_name<<"\t\t"<<employee[i].employee_id<<"\t\t"<<employee[i].phone_number<< "\n ";
    cout << endl;
    
}
void getEmployeeLast(Data employee[], int& count) {
    
    string searchName;

    cout << "Please enter the last name of the employee you want to search for.\n";
    cin >> searchName;
    /*
    for (int i = 0; i < count; i  )
        if (searchName ==employee[i].last_name) {
                       
            cout << employee[i].last_name;
        }
        */

    string matchString = searchName;
    for (int i = 0; i < count; i  ) {
        if (employee[i].last_name.find(matchString, 0) != std::string::npos) {
            cout << employee[i].last_name << endl;
        }
        else {
            cout << "No employee was located with that name.\n";
            exit(1);
        }
    }

}
Data getAddEmployee(Data &employee, int& count) {

    string firstName, lastName, phoneNumber;
    int employeeID, size;

    for (int i = 0; i < count; i  )
        cout << employee[i].first_name << "\t\t\t" << employee[i].last_name << "\t\t" << employee[i].employee_id << "\t\t" << employee[i].phone_number << "\n ";
    cout << endl;

    cout << count <<"\n";
    size = count   1;
    cout << size << "\n";

    cout << "Please enter the new employee First Name.\n";
    cin >> firstName;

    cout << "Please enter the new employee Last Name.\n";
    cin >> lastName;

    cout << "Please enter the new employee's ID.\n";
    cin >> employeeID;

    cout << "Please enter the new employee Phone Number.\n";
    cin >> phoneNumber;

    employee[10].first_name = firstName;
    employee[10].last_name = lastName;
    employee[10].employee_id = employeeID;
    employee[10].phone_number = phoneNumber;
    for (int i = 0; i < size; i  ) {
        cout << employee[i].first_name << "\t\t\t" << employee[i].last_name << "\t\t" << employee[i].employee_id << "\t\t" << employee[i].phone_number << "\n ";
        cout << endl;
    }
    return employee;
}

CodePudding user response:

First of all, your naming could be better. Like employeeList instead employee to specify its a list/array of employee, also addEmployee instead of getAddEmployee.

Second, instead of pasting whole code, it's better to paste the relevant block of code based on your question to make it clearer.

The solution is, your array is basically already on global scope, you don't need to pass it. Can just directly modify the array.

struct Data {
    string first_name;
    string last_name;
    int employee_id;
    string phone_number;
};

void addEmployee();

const int SIZE = 15;
Data employeeList[SIZE];
int current = 0;

int main(){
    Data emp1 = {"John", "Doe", 100, "12345"};
    employeeList[0] = emp1;
    current  ;
    
    addEmployee();
    
    for(int i=0; i<current; i  ){
        Data emp = employeeList[i];
        cout<<i 1<<" "<<emp.first_name<<" "<<emp.phone_number<<endl;
    }
}

void addEmployee(){
    Data emp2 = {"Sam", "Smith", 200, "67890"};
    employeeList[current] = emp2;
    
    current  ;
}

Can try it here: https://onlinegdb.com/drhzEOpis

  • Related