title: Read from a text file and storing to an array C++
body: <p>I have a program that reads values from a text file(sortarraysin.txt) and stores those values into an array. However when I try to print the array to the console my output does not display the numbers from the text file. My text file and program are shown below.</p>
  
  <p>Text file:</p>
  
  <pre><code>45 59 302 48 51 3 1 23
  </code></pre>
  
  <p>Program: </p>
  
  <pre><code>int array[8];
  int i = 0;
  string inFileName, getcontent;
  cout &lt;&lt; "Enter input file name -&gt; ";
  cin &gt;&gt; inFileName;
  fstream inFileStr(inFileName.c_str(), ios::in);
  if(inFileStr.fail()){
      cerr &lt;&lt; "Unable to open " &lt;&lt; inFileName &lt;&lt; endl;
      exit(-1);
  }
  if(inFileStr.is_open()){
      while(!inFileStr.eof()){
          getline(inFileStr, getcontent);
          cout &lt;&lt; getcontent &lt;&lt; endl;
          array[i++] &lt;&lt; atoi(getcontent.c_str());
          for(i=0;i&lt;=8;i++){
          cout &lt;&lt; array[i] &lt;&lt; " ";
          }
      }
  }
  </code></pre>
  
  <p>Output: </p>
  
  <pre><code>Enter input file name -&gt; sortarraysin.txt 
  45 59 302 48 51 3 1 23
  -2145069216 1 -13232 0 -2145069216 1 -12816 0 -13136
  </code></pre>
  
  <p>Why are my array values printing these numbers instead of the values from the text file?</p>