title: Merging Two Text Files Sorted
body: <p>I need to merge two text files and have them sort by "lastName" on a third outputted file. My code is below, it is outputting gibberish, all on one line. I understand my overload may be stupid, any help is appreciated.</p>
  
  <pre><code>//header
  #include &lt;fstream&gt;
  #include &lt;string&gt;
  #include &lt;iostream&gt; 
  using namespace std;
  
  struct mergedList {
    string firstName;
    string lastName;
    float gpa;
    int hours;
  };
  
  ostream&amp; operator &lt;&lt; (ostream&amp; os, mergedList A) {
    os &lt;&lt; A.firstName &lt;&lt; "\t" &lt;&lt; A.lastName &lt;&lt; "\t" &lt;&lt; A.gpa &lt;&lt; "\t" &lt;&lt; A.hours;
    return os;
  }
  istream&amp; operator &gt;&gt; (istream&amp; is, mergedList&amp; A) {
    is &gt;&gt; A.firstName &gt;&gt; A.lastName &gt;&gt; A.gpa &gt;&gt; A.hours;
    return is;
  }
  
  void swap(mergedList D1[], int i, int j);
  void sort(mergedList D1[], int size);
  
  int main() {
    ifstream indata;
    ifstream indata2;
    ofstream outdata;
    indata.open("merge1.txt");
    indata2.open("merge2.txt");
    outdata.open("merged.txt");
    //begin sentinel controlled loop for both lists
    mergedList D1[100];
    int index = 0;
    indata &gt;&gt; D1[index];
    while (!indata.eof()) {
      index++;
      indata &gt;&gt; D1[index];
    }
    sort(D1, index);
    mergedList D2[100];
    int index2 = 0;
    indata2 &gt;&gt; D2[index2];
    while (!indata2.eof()) {
      index2++;
      indata2 &gt;&gt; D2[index2];
    }
    sort(D2, index); {
      int i = 0, j = 0;
      while ((i &lt; index) &amp;&amp; (j &lt; index2)) if (D1[i].lastName &lt; D2[j].lastName) {
          outdata &lt;&lt; D1[i];
          i++;
          indata &gt;&gt; D1[i];
        } else {
          outdata &lt;&lt; D2[j];
          j++;
          indata2 &gt;&gt; D2[j];
        }
    }
    indata.close();
    indata2.close();
    outdata.close();
    return 0;
  }
  
  void swap(mergedList D1[], int i, int j) {
    mergedList temp;
    temp = D1[i];
    D1[i] = D1[j];
    D1[j] = temp;
    return;
  }
  
  void sort(mergedList D1[], int size) {
    for (int p = 1; p &lt; size; p++) {
      for (int c = 0; c &lt; size - p; c++) {
        if (D1[c].lastName &gt; D1[c + 1].lastName) swap(D1, c, c + 1);
      }
    }
    return;
  }
  </code></pre>