title: Calculate sum of int inputs using a for statement
body: <p>I've just started (yesterday) learning how to code in C++ and I'm following Lippman's 'C++ Primer' book.</p>
  
  <p>He gives us this code bit:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main()
  
  {
  
      int sum = 0, value = 0;
  
      // read until end-of-file, calculating a running total of all values read
  
      while (std::cin &gt;&gt; value)
  
          sum += value; // equivalent to sum = sum + value
  
      std::cout &lt;&lt; "Sum is: " &lt;&lt; sum &lt;&lt; std::endl;
  
      return 0;
  
  }
  </code></pre>
  
  <p>Works fine. Now, one of the exercises is to write my own version of a program that prints the sum of a set of integers read from cin. I'm trying to do that using a for statement. Right now I have this:</p>
  
  <pre><code>#include &lt;iostream&gt;
  
  int main(){
  
      int sum = 0;
  
      std::cout &lt;&lt; "Please insert your values. When you want to terminate, insert the number 0." &lt;&lt; std::endl;
  
  
      for(int val1; val1 != 0; std::cin &gt;&gt; val1)
          sum += val1;
  
      std::cout &lt;&lt; "The sum of your inputs is " &lt;&lt; sum &lt;&lt; std::endl;
  
      return 0;
  
  }
  </code></pre>
  
  <p>To avoid having to crtl + z to end the program, I'm trying to add a condition in which when the user inputs the number zero the program outputs the sum of all the inputs and terminates. For some reason, the sum I get is always wrong, i.e., I input 9 and 9 and the sum outputs 16.</p>
  
  <p>In summarizing, I want the user to keep inputing numbers until he doesn't want to anymore (inputs the number zero), at which point the program calculates the sum of all former inputs and outputs the result.</p>
  
  <p>Thanks in advance for the help.</p>