title: How to simply validate correctly in c++?
body: <p>I am new to c++ - and I am having trouble to validate user that the input must be a integer, and I just want the basic - simpler way, for me to understand. (Just basic way) to validate the input of the user How can I make it to work prior to my code below. Thank you for you're help and you're time. For example: if I get prompt to enter positive number and Input the letter x that means it will show "Invalid entry".. thank you!</p>
  
  <p>Here is my code:</p>
  
  <p><strong>Updated</strong></p>
  
  <pre><code>     if (cin &gt;&gt; x &amp;&amp; x &lt; 0) {
  
          } else {
               cout &lt;&lt; "Invalid entry, Try again." &lt;&lt; endl;
              cin.clear();
              while (cin.get() != '\n');  
          } 
  </code></pre>
  
  <hr>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;fstream&gt; // for file stream. 
  
   using namespace std;
  
  int main() {
  
      // Variables 
      int x, reversedNumber, remainder;
  
      // Creating String variables and setting them to empty string. 
      string even = "", odd = "";
  
      // Creating a file. 
      ofstream out;
      out.open("outDataFile.txt");
  
      // creating a character variable 
      // for the user input if they want to use the program again. 
      char ch;
  
      do {
          // Even number. 
          even = "";
  
          // Odd number.
          odd = "";
  
          // Reversed number
          reversedNumber = 0;
  
          // Prompt the user to enter a positive integer. 
          cout &lt;&lt; "\nEnter a positive integer and press &lt;Enter&gt; ";
  
  
  
          // Validate user input. 
            while (cin &gt;&gt; x || x &lt; 1) {
  
              // Clear out the cin results.
             cin.clear();
  
            // Display user that it is not a positive integer. 
          cout &lt;&lt; "Invalid entry, Try again. ";
  
        }
          // Display Results to the screen
          cout &lt;&lt; "the original number is " &lt;&lt; x &lt;&lt; "\n";
  
          // Display results in the text file. 
          out &lt;&lt; "the original number is " &lt;&lt; x &lt;&lt; "\n";
  
          // Display number reversed. 
          cout &lt;&lt; "the number reversed ";
  
          // Display number reversed in text file. 
          out &lt;&lt; "the number reversed ";
  
          //Reversing the integer. 
          while (x != 0) {
              remainder = x % 10;
  
              reversedNumber = reversedNumber * 10 + remainder;
  
              // Display on screen 
              cout &lt;&lt; remainder &lt;&lt; " ";
  
              // Display in text file. 
              out &lt;&lt; remainder &lt;&lt; " ";
  
              x /= 10;
  
          }
  
          // Display the results on screen and in the text file. 
          cout &lt;&lt; "\n";
          out &lt;&lt; "\n";
  
  
          // Reading the reverse numbers result.
          while (reversedNumber != 0) {
  
              remainder = reversedNumber % 10;
  
  
              // Checking if the number is even or odd. 
              if (remainder % 2 == 0) {
  
                // even = even * 10 + remainder;
  
  
              } else  {
  
  
                //  odd = odd * 10 + remainder;
  
  
  
              }
  
              reversedNumber /= 10;
  
          }
  
          //Displaying the even numbers. 
  
          if (even != "") {
  
              cout &lt;&lt; "the even digits are " &lt;&lt; even &lt;&lt; "\n";
  
  
              out &lt;&lt; "the even digits are " &lt;&lt; even &lt;&lt; "\n";
  
  
          }          
  
          // If it is not even then display.. 
          else {
  
              cout &lt;&lt; "There are no even digits \n";
  
              out &lt;&lt; "There are no even digits \n";
  
          }
  
          //Display the odd results. 
  
          if (odd != "") {
  
              cout &lt;&lt; "the odd digits are " &lt;&lt; odd &lt;&lt; "\n";
  
              out &lt;&lt; "the odd digits are " &lt;&lt; odd &lt;&lt; "\n";
  
          }           
           // If its not odd then display. 
          else {
  
              cout &lt;&lt; "There are no odd digits \n";
  
              out &lt;&lt; "There are no odd digits \n";
  
          }
  
          // just a divider to divide the results inside text file.
          out &lt;&lt; "----------------- \n";
  
          // Prompt the user if they want to use the program again.
          cout &lt;&lt; "\nDo you like to continue/repeat? (Y/N):";
  
          // get the input from user. 
          cin &gt;&gt; ch;
  
          if ((ch == 'Y') || (ch == 'y')) {
  
          } else {
              cout &lt;&lt; "\nGoodbye!" &lt;&lt; endl;
          }
  
      } while (ch == 'y' || ch == 'Y');
  
  
      // close the text file. 
  
      out.close();
  
      return 0;
  
  }
  </code></pre>