title: C++ Reading multiple integers from a text file
body: <p>I'm a beginning C++ student and am having difficulty in getting my code to read multiple integers from a text file.</p>
  
  <p>My task is to take pre existing code (the code I posted below) and modify it so instead of taking a user's input, it reads from a text file.  </p>
  
  <p>The main issue i'm having lies in the void getNumber function.  It originally was just a simple cin statement that read 20 numbers input by the user, carried them into the main function, processed them in another function, then prints the results in the last one.  </p>
  
  <p>I have modified the code to read the text file successfully, however, it only reads the first value of the text file (ex text file reads: 1 2 3 4 ... 20) and the output reads: 1 1 1 1 1...</p>
  
  <p>I've been looking up solutions for hours, unfortunately, a lot of the responses call for things I am not familiar with.  It seems I will need to establish some type of loop?  I'm unsure.  </p>
  
  <p>Thank you!</p>
  
  <pre><code>#include &lt;iostream&gt; 
  #include &lt;iomanip&gt;
  #include &lt;fstream&gt;
  
  using namespace std;
  
   const int N = 20;   
  
      //Function prototypes
  void initialize(int&amp; zeroCount, int&amp; oddCount, int&amp; evenCount);
  void getNumber(int&amp; num);
  void classifyNumber(int num, int&amp; zeroCount, int&amp; oddCount, 
                      int&amp; evenCount);
  void printResults(int zeroCount, int oddCount, int evenCount);
  
  int main ()
  {
          //Variable declaration
      int counter; //loop control variable 
      int number;  //variable to store the new number 
      int zeros;   //variable to store the number of zeros 
      int odds;    //variable to store the number of odd integers 
      int evens;   //variable to store the number of even integers 
  
  
  
      initialize(zeros, odds, evens);                 //Step 1
  
      cout &lt;&lt; "Please enter " &lt;&lt; N &lt;&lt; " integers."
           &lt;&lt; endl;                                   //Step 2
      cout &lt;&lt; "The numbers you entered are: "
           &lt;&lt; endl;
  
      for (counter = 1; counter &lt;= N; counter++)      //Step 3
      {
          getNumber(number);                          //Step 3a
          cout &lt;&lt; number &lt;&lt; " ";                      //Step 3b
          classifyNumber(number, zeros, odds, evens); //Step 3c
      }// end for loop 
  
      cout &lt;&lt; endl;
  
      printResults(zeros, odds, evens);               //Step 4
  
      return 0;
  }
  
  void initialize(int&amp; zeroCount, int&amp; oddCount, int&amp; evenCount)
  {
      zeroCount = 0;
      oddCount = 0;
      evenCount = 0;
  }
  
  void getNumber(int&amp; num)
  {
      ifstream inData;
  
  
      inData.open("inputfile.txt");
      inData &gt;&gt; num;
  
  
  
  
  }
  
  void classifyNumber(int num, int&amp; zeroCount, int&amp; oddCount,
                      int&amp; evenCount)
  {
      switch (num % 2)
      {
      case 0: 
          evenCount++;  
          if (num == 0)
              zeroCount++;  
          break;
     case 1: 
     case -1: 
         oddCount++;
     } //end switch
  } //end classifyNumber
  
  void printResults(int zeroCount, int oddCount, int evenCount)
  { 
  
      ofstream outData;
  
  
      outData.open("outputfile.txt.");
  
      outData &lt;&lt; "There are " &lt;&lt; evenCount &lt;&lt; " evens, "
           &lt;&lt; "which includes " &lt;&lt; zeroCount &lt;&lt; " zeros"
           &lt;&lt; endl;
  
      outData &lt;&lt; "The number of odd numbers is: " &lt;&lt; oddCount
           &lt;&lt; endl;
      outData.close();
  } //end printResults
  </code></pre>