how to use array data member of struct in main in C++?

Viewed 69

If i have a struct Emp have 2 data member no_of_emp[5]; and department_name how i use no of emp array in main i am little confuse about please help me. may be my question are not good.

struct emp
{
  int no_of_emp[5];
  string department_name;
 
}
3 Answers

You can use it in your main with brace initialisers:

std::vector<emp> employers =
{
    {
        {0, 1, 2, 3, 4},
        "department1"
    },
    {
        {5, 6, 7, 8, 9},
        "department2"
    }
};

then you can use your list of employers, for example:

for (const auto& x: employers)
{
    for (const auto num: x.no_of_emp)
        cout << num << " ";

    cout << x.department_name << endl;
}

Here we've just used a little bit of vector concept:

#include <iostream>
#include <vector>

struct emp {
    int no_of_emp[5]; // it'll store 5 elements in each struct array
    std::string department_name;
};

int main(void) {
    std::vector<emp> e;
    emp temp; // to accept values first and then add them later together
    const int max = 40; // set the maximum employee to iterate till

    // getting the values
    for (int i = 0; i < max; i++) {
        std::cout << "=== Employee " << i + 1 << std::endl;

        for (int i = 0; i < 5; i++) { // accept 5 numbers for each
            std::cout << "No. of employees: ";
            std::cin >> temp.no_of_emp[i];
        }

        std::cout << "Department name: ";
        fseek(stdin, 0, SEEK_END); // to avoid skipping line

        std::getline(std::cin, temp.department_name);

        e.push_back(temp); // pushing the values containing in the initialized struct
    }

    std::cout << std::endl;

    for (int i = 0; i < max; i++) {
        std::cout << "--- Employee " << i + 1 << std::endl;

        for (const auto i : e[i].no_of_emp) // getting those 5 elements of each array
            std::cout << i << ' ';
        
        std::cout << e[i].department_name << std::endl;
    }

    return 0;
}

We've taken a temporary struct temp to retrieve all the required values from the user and then push them back together into the vector till max. After that, simply we print each of the struct.

You should then get the similar output:

$ g++ -o main main.cpp && ./main

=== Employee 1              // getting values
No. of employees: 3 2 3 4 3
Department name: Something
=== Employee 2
No. of employees: 1 2 4 3 3
Department name: Else

--- Employee 1             // displaying values
3 2 3 4 3 Something        // by retrieving from arrays of struct
--- Employee 2             // using vectors (format: x x x x x department)
1 2 4 3 3 Else             // till 40 times (as per of the requirement)

Something like this:

struct emp e; //declare a variable of type struct emp

e.no_of_emp[0] = 25; //set the value of the first element of no_of_emp array

for (int i = 0; i < 5; i++){ //or set all the elements in no_of_emp array
    e.no_of_emp[i] = i + 1; //prints 1
}

printf("%d ", e.no_of_emp[0]); // print the first element no_of_emp array

for (int i = 0; i < 5; i++){ //or print all the elements no_of_emp array
    printf("%d ", e.no_of_emp[i]); //prints 1 2 3 4 5
}

for (auto& i : e.no_of_emp){ //or with a range based loop
    printf("%d", i);
}

C++ provides a fixed size array container, std::array you can use instead of a C style array:

struct emp
{
  std::array<int, 5> no_of_emp;
  string department_name;
 
};

The code to access and set the elements can be the same.


Or if you need a variable lenght array you could use std::vector

Here the setting of the elements is a bit different:

struct emp
{
  std::vector<int> no_of_emp;
  string department_name;
 
};
e.no_of_emp.push_back(25); //set the value of no_of_emp of the first element of the array

for (int i = 0; i < 5; i++){ //or set all the no_of_emp of the structs in the array
    e.no_of_emp.push_back(i + 1);
}

The access can the same as if it was a C-style array like showed in the first sample code.

Related