title: Writing a C++ calculator program that runs one more instance than needed
body: <p>So for class project I need to create a calculator program that takes input from a text file <code>formula.txt</code> which contains the following formulas:</p>
  
  <p>'15 ;<br>
  10 + 3 + 0 + 25 ;'</p>
  
  <p>When the program runs it should calculate and print the results of the formulas, with line break as such:   </p>
  
  <p><code>15
  38</code></p>
  
  <p>However, every time I run the program, it gives me the following result:<br>
  <code>15
  38
  25</code></p>
  
  <p>I've worked through my code and haven't been able to find a problem. Any help would be appreciated. By code is found below.  </p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main ()
  {
    double input; //initialize input variable
    char sign; //intialize sign character
    double calc = 0; //initial calculation value set to 0
    bool add= true; // add to use whether to add or not
    bool cont = true; // boolean for continuing loop
  
    while (cont) //loop only continues while cont is true
    {
  
      cin &gt;&gt; input; //take in input
  
      if (add)  //if add is true
      {
        calc = calc + input; // adds the input to calc
      }
  
      else //if add is false
      {
        calc = calc - input; //subtracts input from calc
      }
  
      cin &gt;&gt; sign; //take in sign
  
      if (sign == '+') //if sign is '+'
      {
        add = true; //add is true
      }
  
      else if (sign == '-') //if sign is '-'
      {
        add = false; //add is false
      }
  
      else if (sign == ';') //if sign is ';'
      {
        cout &lt;&lt; calc &lt;&lt; endl; //outputs calc to console
        calc = 0;
      }
  
      if (cin.fail()) // if cin fails
      {
        cont = false; //continue is set to false
      }
  
    }
    return 0;
  }
  </code></pre>