title: How to get the last integer of sequence of integers separated by commas in C++?
body: <p>What I'm trying to do is to get each integer from a file of integers separated by commas and add them. For example, if the file contains <code>2,3,4,1</code> my program has to display that the sum is 10. For that, I wrote the following code.</p>
  
  <pre><code>int number_sum(const char* filename) {
  
  std::ifstream file(filename); 
  
  if (file.is_open()) {
  
      int sum = 0; //sum
      char c; //Store each comma
      int number = 0; //Store each number
  
      while ( (file &gt;&gt; number &gt;&gt; c) &amp;&amp; (c == ',') ) {
  
          std::cout &lt;&lt; "Adding up: " &lt;&lt; number &lt;&lt; " + " &lt;&lt; sum &lt;&lt; std::endl;
          sum = sum + number;
  
  
      }
  
      std::cout &lt;&lt; "Result is: " &lt;&lt; sum &lt;&lt; std::endl;
  
      return sum;
  }
  else {
      std::cout &lt;&lt; "ERROR" &lt;&lt; std::endl;
      return -1;
  }
  </code></pre>
  
  <p>This works well with all the digits except the last one. The problem comes because the last one is not followed by a comma, so the program does not get it. I've tried to discriminate "c" value by checking whether it is ',' or EOF, but this didn't work. Is there any solution so that my program can get the last number and add it up to the rest? (Sorry for my english, not native).</p>
  
  <p>Thank you very much in advance.</p>