title: Infinite Loop in C++ function
body: <p>I have written a very little program that adds two integers:</p>
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int inpInt() {            //ask for an input in integer format
      int number = 0;
      cin &gt;&gt; number;       
      while (!cin) {        //keep asking if the input was not an integer
          cin &gt;&gt; number;
      }
      return number;
  }
  
  int main() {
      int summand1, summand2, sum;
      cout &lt;&lt; &quot;Summand 1: &quot;; summand1 = inpInt();
      cout &lt;&lt; &quot;Summand 2: &quot;; summand2 = inpInt();
      sum = summand1 + summand2;
      cout &lt;&lt; &quot;Sum: &quot; &lt;&lt; sum &lt;&lt; &quot;\n&quot;;
  }
  </code></pre>
  <p>The problem is that when I do not enter an integer, I am trapped in an infinite loop where I cannot enter neither an integer nor anything else. The strange thing is that when I include the integer testing loop in main(), the code works.</p>
  <p>Thank you very much for any help!</p>