title: how to restricte input to make sure it is a number with cout and cin in c++?
body: <p>I'm new to C++ and got confused about the following code which is excerpted from the book a complete guide to C++. </p>
  
  <pre><code>//Ref2.cpp
  //Demonstrating functions with parameters
  //of reference type.
  //--------------------------------------------------
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  using namespace std;
                                                                         // Prototypes:
  bool getClient( string&amp; name, long&amp; nr);
  void putClient( const string&amp; name, const long&amp; nr);
  
  
  
  
  int main()
  {
      string clientName;
      long clientNr;                                                    // client number?
      cout &lt;&lt; "\nTo input and output client data \n"
           &lt;&lt; endl;
      if( getClient( clientName, clientNr))
  
          putClient( clientName, clientNr);
      else
          cout &lt;&lt; "Invalid input!" &lt;&lt; endl;
      return 0;
  }
  
  // getClient function definition
  bool getClient( string&amp; name, long&amp; nr)                 // pass by reference, both name and nr are references to the function arguments clientName and clientNr
  {
       cout &lt;&lt; "\nTo input client data!\n"
            &lt;&lt; " Name: ";
       if( !getline( cin, name)) return false;
       cout &lt;&lt; " Number: ";
       if( !( cin &gt;&gt; nr)) return false;
       return true;
  }
  
  
  
  // putClient function Definition
  void putClient( const string&amp; name, const long&amp; nr)  // pass by reference, both name and nr are references to the function arguments clientName and clientNr
  {
       cout &lt;&lt; "\n-------- Client Data ---------\n"
            &lt;&lt; "\n Name:"; 
       cout &lt;&lt; name &lt;&lt; "\n Number: "; 
       cout &lt;&lt; nr &lt;&lt; endl;
  }
  </code></pre>
  
  <p>with the above code, the following is the output of running it. </p>
  
  <pre><code>To input and output client data 
  
  
  To input client data!
   Name: fjdkljfdlfjdlfjdljf dfjdlfjdlfjdlfjdl dfj
   Number: 123435jjjjj
  
  -------- Client Data ---------
  
   Name:fjdkljfdlfjdlfjdljf dfjdlfjdlfjdlfjdl dfj
   Number: 123435
  </code></pre>
  
  <p>My question is: the user input <code>12345jjjjj</code> for the client Number entry. How come when printing out, it has changed into 12345?</p>