title: C++ Array Input from File
body: <p>The code below is designed two input two separate .txt files. Each file has a different integer on each line. The code below reads the files, and stores each value in the index off an array. The input works great, and the cout in the while loop prints the proper value to command line. </p>
  
  <p>The issue however comes with the cout &lt;&lt; setTwo[0] &lt;&lt; endl; outside of the loop. It prints random values as if the index of the array is not set. Im totally confused. </p>
  
  <p>set2.txt</p>
  
  <pre><code>1
  3
  9
  2
  </code></pre>
  
  <p>Code</p>
  
  <pre><code>int maxSize = 100;
      int setOne[maxSize];
      int setTwo[maxSize];
      int setOneSize = 0;
      int setTwoSize = 0;
  
  //open files
  ifstream fileOne ("set1.txt");
  ifstream fileTwo ("set2.txt");
  
  int number;
  int number2;
  
  fileOne &gt;&gt; number;
  fileTwo &gt;&gt; number2;
  
  //declare arrays
  for(int i = 0; i &lt; maxSize; i++) {
      while (fileOne.good()) {
          setOne[i] = number;
          cout &lt;&lt; "set 1 " &lt;&lt; setOne[i] &lt;&lt; endl;
          fileOne &gt;&gt; number;
          setOneSize += 1;
      }
  
      while (fileTwo.good()) {
          setTwo[i] = number2;
          cout &lt;&lt; "set 2 " &lt;&lt; setTwo[i] &lt;&lt; endl;
          fileTwo &gt;&gt; number2;
          setTwoSize += 1;
  
      }
  }
  
  cout &lt;&lt; setTwo[0] &lt;&lt; endl;
  </code></pre>