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;  
}

>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.