title: C++ Bubble Sorting with structure members and pointers
body: <p>I have to organize my array with a bubble sort to display who works the most hours to who works the least amount of hours. After that, I need to display the employee who makes the most amount of pay in the company. However, I'm not sure how I would go about using a bubble sort with pointer variables and a structure member. At the moment I have a generic code for my bubble sort in the arraySort function but it needs improvement. Thank you!</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  
  using namespace std; 
  
  struct incomeInfo {
      string id;
      string name;
      int hours;
      double hRate;
      double regPay = 0;
      double otPay = 0;
  };
  
  const int ARRAY_SIZE = 25; 
  incomeInfo income[ARRAY_SIZE]; 
  
  void getIncome(incomeInfo[], int&amp;);
  void compute(incomeInfo *, int);
  void display(incomeInfo[], int);
  void summary(incomeInfo[], int);
  void sortArray(incomeInfo[], int);
  void mostPay(incomeInfo[], int);
  
  int main()
  {
  
      incomeInfo income[ARRAY_SIZE];
      int count = 0;                          
  
      getIncome(income, count);
      compute(income, count);
      display(income, count);
      summary(income, count);
      sortArray(income, count);
      mostPay(income, count);
  
      return 0;
  }
  
  void getIncome(incomeInfo income[], int &amp;count)
  {
  
      ifstream inputFile;                 
      char line[50];                      
  
  
      inputFile.open("Payroll.txt");
  
  
      if (inputFile.fail())
      {
          cout &lt;&lt; "\n\n\tError openning file: " &lt;&lt; "\n\n\t";
          system("pause");
          exit(1);
      }
  
      else
      {
          while (!inputFile.eof())    
          {
              inputFile.getline(line, 50, ',');        
              income[count].id = line;
              inputFile.getline(line, 50, ',');
              income[count].name = line;
              inputFile.getline(line, 50, ',');
              income[count].hours = atoi(line);       
              inputFile.getline(line, 50, ',');
              income[count].hRate = atof(line);           
  
              count++;
          }
      }
      inputFile.close();
  
      return;
  }
  
  
  void compute(incomeInfo *ptrI, int count)    
  {                                                
      for (int i = 0; i&lt;count; i++)
  
      if (ptrI-&gt;hours &lt;= 40)
      {
          ptrI-&gt;regPay = ptrI-&gt;hours * ptrI-&gt;hRate;
          ptrI++;
      }
  
      else if (ptrI-&gt;hours &gt; 40)
      {
          ptrI-&gt;regPay = 40 * ptrI-&gt;hRate;
          ptrI-&gt;otPay = (ptrI-&gt;hours - 40) * (ptrI-&gt;hRate + (ptrI-&gt;hRate* .5));
          ptrI++;
      }
          return;
  }
  
  
  void display(incomeInfo income[], int count)
  {
      cout &lt;&lt; fixed &lt;&lt; showpoint &lt;&lt; setprecision(2);
      cout &lt;&lt; setw(15) &lt;&lt; left &lt;&lt; "ID" &lt;&lt; setw(16) &lt;&lt; "Name";
      cout &lt;&lt; left &lt;&lt; setw(8) &lt;&lt; "Hours" &lt;&lt; setw(14) &lt;&lt; "Hourly Rate" &lt;&lt; setw(14) &lt;&lt; "Regular Pay" &lt;&lt; setw(14) &lt;&lt; "Overtime Pay" &lt;&lt;endl;
  
  
      for (int i = 0; i &lt; count; i++)
      {
          cout &lt;&lt; setw(14) &lt;&lt; left &lt;&lt; income[i].id &lt;&lt; setw(15) &lt;&lt; income[i].name;
          cout &lt;&lt; right &lt;&lt; setw(6) &lt;&lt; income[i].hours &lt;&lt; setw(12) &lt;&lt; income[i].hRate;
          cout &lt;&lt; setw(14) &lt;&lt; income[i].regPay &lt;&lt; setw(14) &lt;&lt; income[i].otPay &lt;&lt; endl;
      }
  
      return;
  }
  
  
  void sortArray(incomeInfo income[], int count)
  {
      {
          bool swap;
          int temp;
  
          do
          {
              swap = false;
              for (int count = 0; count &lt; (size - 1); count++)
              {
                  if (array[count] &gt; array[count + 1])
                  {
                      temp = array[count];
                      array[count] = array[count + 1];
                      array[count + 1] = temp;
                      swap = true;
                  }
              }
          } while (swap);
      }
  
  }
  
  
  void summary(incomeInfo income[], int count)
  {
          cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Total payroll amount for the company = $";
          cout &lt;&lt; (income[0].regPay + income[0].otPay) + (income[1].regPay + income[1].otPay) + (income[2].regPay + income[2].otPay) + (income[3].regPay + income[3].otPay) + (income[4].regPay + income[4].otPay) &lt;&lt; endl;
  }
  
  
  void mostPay(incomeInfo[], int count)
  {
      cout &lt;&lt; endl &lt;&lt; endl &lt;&lt; "Employee who earns most money: ";
  }
  </code></pre>