title: How to properly use cin.peek()
body: <p>This function is supposed to read a fraction and place it in an array.  If the user enters '0' the function is supposed to exit.  I am trying to do this using the cin.peek() function but execution always goes into the if statement and doesn't allow the user to exit.</p>
  
  <p>How should I properly code this (I am open to not using peek(), I thought it was the simplest way of doing it.)</p>
  
  <p>Thanks!</p>
  
  <pre><code>void enterFrac(Fraction* fracs[], int&amp; index)
      {
          int n, d;
          char c, slash;
          cout &lt;&lt; "Enter fractions (end by entering a 0): ";
          c = cin.peek();
  
          if ( c != '0')
          {
              cin &gt;&gt; n &gt;&gt; slash &gt;&gt; d;
              Fraction* f = new Fraction();
              f-&gt;num = n;
              f-&gt;den = d;
              fracs[index] = f;
              index++;
          }
      }
  </code></pre>
  
  <p>This test of peek() works however:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main () {
    char c;
    int n;
    char str[256];
  
    cout &lt;&lt; "Enter a number or a word: ";
    c=cin.peek();
  
    if ( (c &gt;= '0') &amp;&amp; (c &lt;= '9') )
    {
      cin &gt;&gt; n;
      cout &lt;&lt; "You have entered number " &lt;&lt; n &lt;&lt; endl;
    }
    else
    {
      cin &gt;&gt; str;
      cout &lt;&lt; " You have entered word " &lt;&lt; str &lt;&lt; endl;
    }
  
    return 0;
  }
  </code></pre>