title: Reading data from a file into a struct, sorting the data and writing to a file
body: <p>I've tried nearly everything. Just looking for some tips. </p>
  
  <p>The project is to read data from a file ["racers2011.txt"] into a struct and sort the race times for the males and sort the racetimes for the females. Group the males and the females serperately and output the with their rank and racetime, being their best blue race and their best red race combined. I have read the file in and output it to the new file, but cannot figure out how to sort the file.</p>
  
  <p>If someone could help me out in the slightest it would be greatly appreciated.</p>
  
  <p>Here is the code I have so far (some of the code I have does not compile so I have commented it out):</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;cstring&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
  struct Racer_struct
  {
      int bib;
      char sex;
      char fname[30];
      char lname[30];
      double b1, b2, r1, r2;
  };
  
  bool connectInFile(ifstream&amp; fin, char infilename[]);
  
  bool connectOutFile(ofstream&amp; fout, char outfilename[]);
  
  void readData(ifstream&amp; fin, Racer_struct racers[], const int&amp; MAX);
  
  //void racerGender(ostream&amp; fout, Racer_struct racers[], const int&amp; MAX);
  
  //double calcTotalTime(Racer_struct racers[], double total[], const int&amp; MAX);
  
  void writeData(ostream&amp; fout, Racer_struct racers[], const int&amp; MAX);
  
  int main()
  {
      const int MAX = 38;
      Racer_struct racers[MAX];
  //    double total[MAX];
  
      ifstream fin;
      ofstream fout;
      char in_file[30], out_file[30];
      bool opened;
      char title[79];
  
      opened = connectInFile(fin, in_file);
      cout &lt;&lt; opened &lt;&lt; endl;
      opened = connectOutFile(fout, out_file);
      cout &lt;&lt; opened &lt;&lt; endl;
  
      if(opened)
      {
          cout &lt;&lt; "CONNECTED to: " &lt;&lt; in_file &lt;&lt; endl;
          cout &lt;&lt; "WRITING to: " &lt;&lt; out_file &lt;&lt; endl;
  
          for(int i=0; i&lt;=3; i++)
          {
              fin.getline(title, 80);
              fout &lt;&lt; title &lt;&lt; "\n";
          }
      }
      readData(fin, racers, MAX);
      writeData(fout, racers, MAX);
  
      fin.close();
      fout.close();
      cout &lt;&lt; endl;
      return 0;
  }
  
  bool connectInFile(ifstream&amp; fin, char infilename[])
  {
      bool success = true;
      cout &lt;&lt; "Enter input filename: ";
      cin &gt;&gt; infilename;
      fin.open(infilename);
      if(fin.fail())
          success = false;
      return success;
  }
  
  bool connectOutFile(ofstream&amp; fout, char outfilename[])
  {
      bool opened = true;
      cout &lt;&lt; "Enter the filename you wish to write to: ";
      cin &gt;&gt; outfilename;
      fout.open(outfilename);
      if(fout.fail())
          opened = false;
      return opened;
  }
  
  void readData(ifstream&amp; fin, Racer_struct racers[], const int&amp; MAX)
  {
      char ws;
  
      for(int i=0; i&lt;MAX &amp;&amp; fin.peek()!= EOF; i++)
      {
          fin &gt;&gt; racers[i].bib &gt;&gt; racers[i].sex &gt;&gt; racers[i].fname &gt;&gt; racers[i].lname
              &gt;&gt; racers[i].b1 &gt;&gt; racers[i].b2 &gt;&gt; racers[i].r1 &gt;&gt; racers[i].r2;
          fin.get(ws);
      }
  }
  
  /*
  void racerGender(ostream&amp; fout, Racer_struct racers[], const int&amp; MAX)
  {
  
      for(int i=0; i&lt;MAX; i++)
          if(racers[i].sex == 'M')
          {
              calcTotalTime(racers, total, MAX);
              writeData(fout, racers, MAX);
          }
          else
          {
              calcTotalTime(racers, total, MAX);
              writeData(fout, racers, MAX);
          }
  }
  
  double calcTotalTime(Racer_struct racers[], double total[], const int&amp; MAX)
  {
      double total[MAX];
  
      for(int i=0; i&lt;MAX; i++)
          if(racers[i].r1 &gt; racers[i].r2 &amp;&amp; racers[i].b1 &gt; racers[i].b2)
              total[i] = racers[i].r2 + racers[i].b2;
          else if(racers[i].r2 &gt; racers[i].r1 &amp;&amp; racers[i].b2 &gt; racers[i].b1)
              total[i] = racers[i].r1 + racers[i].b1;
          else if(racers[i].r1 &gt; racers[i].r2 &amp;&amp; racers[i].b2 &gt; racers[i].b1)
              total[i] = racers[i].r2 + racers[i].b1;
          else
              total[i] = racers[i].b2 + racers[i].r1;
      return total[i];
  }
  */
  
  void writeData(ostream&amp; fout, Racer_struct racers[], const int&amp; MAX)
  {
      for(int i=0; i&lt;MAX; i++)
      {
  
          fout &lt;&lt; racers[i].bib &lt;&lt; "\t" &lt;&lt; racers[i].sex &lt;&lt; "\t" &lt;&lt; racers[i].fname
               &lt;&lt; "\t" &lt;&lt; racers[i].lname &lt;&lt; "\t" &lt;&lt; racers[i].b1 &lt;&lt; "\t" &lt;&lt; racers[i].b2
               &lt;&lt; "\t" &lt;&lt; racers[i].r1 &lt;&lt; "\t" &lt;&lt; racers[i].r2 /*&lt;&lt; "\t" &lt;&lt; total[i]*/ &lt;&lt; endl;
  
  /*      if((i+1)%5)
              fout &lt;&lt; "\t";
          else
              fout &lt;&lt; endl;
  */
      }
  }
  </code></pre>