Thanks in advance for your help. Ok...the last part of this program is where I am stuck. I am supposed to read data in from a file and sort by gender. But the output is supposed to read department, gender, first, last names, job title and salary -- then to display the average salary by gender within each department. So...my question is...how do I sort both department and gender? Here is my code (and it's rather long):

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

//define a structure
struct Employees
{
    string lname, fname, dpt, tit;
    char gen;
    double salary;    
}; 

//define functions 
void bSort(Employees[], int);
void bSort2(Employees[], int);
void bSort3(Employees[], int);
void avg(Employees[], int);
void avg2(Employees[], int);
void avg3(Employees[], int);
void avg4(Employees[], int);
void avg5(Employees[], int);
void avg6(Employees[], int);
void avg7(Employees[], int);

int main()
{
    int count = 0;
    const int size = 22;
    Employees array[22];
    string arr; 
    
    //get info from file 
    ifstream dataIn;
    dataIn.open("Employee Data.txt");
            
    for (int i = 0; i < 22; i++)
    {         
        dataIn >> array[i].lname;
        dataIn >> array[i].fname;
        dataIn >> array[i].gen;
        dataIn >> array[i].dpt;
        dataIn >> array[i].tit;
        dataIn >> array[i].salary;                                                 
    }          
    cout << endl;
    
    //sort info from file
    bSort(array, size);  
    
    //output names in alphabetical order    
    for (int i = 0; i < 22; i++)
    {         
        cout << array[i].lname << " "; 
        cout << array[i].fname << " ";        
        cout << array[i].dpt << " ";                               
        cout << endl;        
    }          
    cout << endl;
    
    //output average salary
    cout << "The average salary for all employees is: ";
    avg(array, size);
    cout << endl;
    cout << endl;    
    
    //sort info from file
    bSort2(array, size); 
    
    //output department(sort by department), first, last names and job title
    for (int j = 0; j < 22; j++)
    {
        cout << array[j].dpt << " ";
        cout << array[j].fname << " ";
        cout << array[j].lname << " ";
        cout << array[j].tit << " ";
        cout << array[j].salary << " ";
        cout << endl; 
    }
    cout << endl;
    
    //output average salary by department
    cout << "The average salary for the Accounting Department is: ";
    avg2(array, size);
    cout << endl;
    cout << "The average salary for the Administration Department is: ";
    avg3(array, size);
    cout << endl;
    cout << "The average salary for the Personnel Department is: ";
    avg4(array, size);
    cout << endl;
    cout << "The average salary for the Sales Department is: ";
    avg5(array, size);
    cout << endl << endl;
    
    //sort info from file
    bSort3(array, size); 
    
    //output department, gender(alphabetical), first, last names, job title, salary
    for (int k = 0; k < 22; k++)
    {
        cout << array[k].dpt << " ";
        cout << array[k].gen << " ";
        cout << array[k].fname << " ";
        cout << array[k].lname << " ";
        cout << array[k].tit << " ";
        cout << array[k].salary << " ";
        cout << endl; 
    }
    cout << endl;    
    
    cout << "The average salary for all females is: ";
    avg6(array, size);
    cout << endl;
    cout << "The average salary for all males is: ";
    avg7(array, size);
    cout << endl << endl;        

    do
    {
       getline(dataIn, arr);
       count++;              
    }while(!dataIn.eof());    
   
         
    dataIn.close();
          
        
    system("pause");
    return 0;
}

void bSort(Employees array[], int size)
{
     Employees emp;
     bool swap;
     
     do 
     {
          swap = false;
          for (int c = 0; c < (size - 1); c++)
          {
              if (array[c].lname > array[c + 1].lname)
              {
                  emp = array[c];
                  array[c] = array[c + 1];
                  array[c + 1] = emp;
                  swap = true;
              }
          }
     }while(swap);
} 

void bSort2(Employees array[], int size)
{
     Employees emp;
     bool swap;
     
     do 
     {
          swap = false;
          for (int c = 0; c < (size - 1); c++)
          {
              if (array[c].dpt > array[c + 1].dpt)
              {
                  emp = array[c];
                  array[c] = array[c + 1];
                  array[c + 1] = emp;
                  swap = true;
              }
          }
     }while(swap);
} 

void bSort3(Employees array[], int size)
{
     Employees emp;
     bool swap;
     
     do 
     {
          swap = false;
          
          for (int c = 0; c < (size - 1); c++)
          {
              if (array[c].gen > array[c + 1].gen)
              {
                  emp = array[c];
                  array[c] = array[c + 1];
                  array[c + 1] = emp;
                  swap = true;
              }
          }
     }while(swap);
} 

void avg(Employees array[], int size)
{
     Employees emp;
     double total = 0;
     double avg;
     
     for (int i = 0; i < size; i++)
         total += array[i].salary;
         
     avg = total / size;  
     
     cout << avg;  
}

void avg2(Employees array[], int size)
{
     Employees emp;
     double total = 0;
     double avg;    
              
     for (int i = 0; i < 9; i++)
        total += array[i].salary;     
         
     avg = total / 9;  
     
     cout << avg;  
}

void avg3(Employees array[], int size)
{
     Employees emp;
     double total = 0;
     double avg;    
               
     for (int i = 9; i < 11; i++)
        total += array[i].salary;     
         
     avg = total / 2;  
     
     cout << avg;  
}

void avg4(Employees array[], int size)
{
     Employees emp;
     double total = 0;
     double avg;    
               
     for (int i = 11; i < 16; i++)
        total += array[i].salary;     
         
     avg = total / 5;  
     
     cout << avg;  
}

void avg5(Employees array[], int size)
{
     Employees emp;
     double total = 0;
     double avg;    
          
     for (int e = 16; e < size; e++)
     {
         total += array[e].salary;     
     }
         
     avg = total / 6;  
     
     cout << avg;  
}

void avg6(Employees array[], int size)
{
     Employees emp;
     double total = 0;
     double avg;    
          
     for (int i = 0; i < 9; i++)
        total += array[i].salary;     
         
     avg = total / 9;  
     
     cout << avg;  
}

void avg7(Employees array[], int size)
{
     Employees emp;
     double total = 0;
     double avg;    
    
     for (int i = 9; i < size; i++)
        total += array[i].salary;     
         
     avg = total / 13;  
     
     cout << avg;  
}

Dani AI

Generated

As pointed out, the cleanest fix is to compare department and gender in a single sort comparator so the array is ordered by department first and gender second (then by name if desired). That removes the need for separate bSort/bSort2/bSort3 passes and for hard-coded index ranges in the many avgX functions. The following shows a compact, modern approach using a std::vector and std::sort plus a single pass to print rows and compute per-department, per-gender averages.

// sort by dept, then gender (normalized), then last/first name
std::sort(employees.begin(), employees.end(),
  [](const Employee& a, const Employee& b){
    auto ga = std::toupper(static_cast<unsigned char>(a.gen));
    auto gb = std::toupper(static_cast<unsigned char>(b.gen));
    return std::tie(a.dept, ga, a.lname, a.fname) < std::tie(b.dept, gb, b.lname, b.fname);
  });

After sorting, scan once to print rows and compute averages by gender inside each department:

std::string curDept = "";
double sumF=0, sumM=0; int cntF=0, cntM=0;

for (const auto& e : employees) {
  if (e.dept != curDept) {
    if (!curDept.empty()) {
      if (cntF) std::cout << curDept << " female avg: " << (sumF/cntF) << '\n';
      if (cntM) std::cout << curDept << " male   avg: " << (sumM/cntM) << '\n';
    }
    curDept = e.dept; sumF = sumM = 0; cntF = cntM = 0;
  }
  std::cout << e.dept << ' ' << e.gen << ' ' << e.fname << ' ' << e.lname << ' '
            << e.title << ' ' << e.salary << '\n';

  if (std::toupper(static_cast<unsigned char>(e.gen)) == 'F') { sumF += e.salary; ++cntF; }
  else { sumM += e.salary; ++cntM; }
}
// print last department averages here

Notes: normalize gender (toupper) and trim/validate input so 'f'/'F' are handled; avoid fixed-size arrays and hard-coded counts; if titles or departments contain spaces, read fields with getline or a structured parser rather than plain >>. Use stable_sort when preserving prior relative order matters. This approach keeps the logic robust, readable, and maintainable compared with multiple bubble sorts and per-range avg functions.

Recommended Answers

All 2 Replies

>how do I sort both department and gender?
The keys are rolled up in a single object, so all you need to do is modify your comparison for the sort. Try comparing both the department and the gender in the same if statement.

It's almost always something simple...thanks Narue!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.