-2

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!

Here is my code:

Updated

     if (cin >> x && x < 0) {

        } else {
             cout << "Invalid entry, Try again." << endl;
            cin.clear();
            while (cin.get() != '\n');  
        } 

#include <iostream>
#include <fstream> // 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 << "\nEnter a positive integer and press <Enter> ";



        // Validate user input. 
          while (cin >> x || x < 1) {

            // Clear out the cin results.
           cin.clear();

          // Display user that it is not a positive integer. 
        cout << "Invalid entry, Try again. ";

      }
        // Display Results to the screen
        cout << "the original number is " << x << "\n";

        // Display results in the text file. 
        out << "the original number is " << x << "\n";

        // Display number reversed. 
        cout << "the number reversed ";

        // Display number reversed in text file. 
        out << "the number reversed ";

        //Reversing the integer. 
        while (x != 0) {
            remainder = x % 10;

            reversedNumber = reversedNumber * 10 + remainder;

            // Display on screen 
            cout << remainder << " ";

            // Display in text file. 
            out << remainder << " ";

            x /= 10;

        }

        // Display the results on screen and in the text file. 
        cout << "\n";
        out << "\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 << "the even digits are " << even << "\n";


            out << "the even digits are " << even << "\n";


        }          

        // If it is not even then display.. 
        else {

            cout << "There are no even digits \n";

            out << "There are no even digits \n";

        }

        //Display the odd results. 

        if (odd != "") {

            cout << "the odd digits are " << odd << "\n";

            out << "the odd digits are " << odd << "\n";

        }           
         // If its not odd then display. 
        else {

            cout << "There are no odd digits \n";

            out << "There are no odd digits \n";

        }

        // just a divider to divide the results inside text file.
        out << "----------------- \n";

        // Prompt the user if they want to use the program again.
        cout << "\nDo you like to continue/repeat? (Y/N):";

        // get the input from user. 
        cin >> ch;

        if ((ch == 'Y') || (ch == 'y')) {

        } else {
            cout << "\nGoodbye!" << endl;
        }

    } while (ch == 'y' || ch == 'Y');


    // close the text file. 

    out.close();

    return 0;

}
Justin S.
  • 55
  • 1
  • 7
  • Possible duplicate of [How to check if the input is a valid integer without any other chars?](https://stackoverflow.com/q/20287186/608639), [Integer validation for input](https://stackoverflow.com/q/16934183/608639), [How can I validate an integer input](https://stackoverflow.com/q/35954159/608639), etc. – jww Jun 28 '18 at 04:05

1 Answers1

1

You could read a string from cin and check each character to make sure that when passed to isdigit() it returns true.

std::string x;
std::cin >> x;
for(char c : x){ //for each char c in string x
   if(!isdigit(c)) //Invalid
}
Salamosaurus
  • 124
  • 4
  • 12
  • @user4581301 What PS. XD, in all seriousness however, i totally agree and think i jumped the gun there. I wanted to try and provide a reason for the downvote (for improvement) and until now did not see the possible duplicate. So my new point to make: read everything – Salamosaurus Jun 28 '18 at 04:12
  • The dupes showed up after you posted and I'm as guilty as you for not completely reading questions. – user4581301 Jun 28 '18 at 04:14
  • I dont want to use `isDigit` is there a easier methods in loops ? – Justin S. Jun 28 '18 at 05:13
  • @JustinS. I suppose instead of isdigit, you could manually check if the character is a number by making sure it is `>= '0' && <= '9'`. [Ascii Table](https://www.asciitable.com/) – Salamosaurus Jun 28 '18 at 13:20
  • check my updated question;.. I am doing something like that. But everytime the user does not enter a positive number, it prints out the error message but again, it shows the rest of the program info. I just want only to print out `Invalid Entry` – Justin S. Jun 29 '18 at 01:52
  • @JustinS. so since you are reading the input straight into an integer. The program is going to convert any characters into it's [integral representation](https://www.asciitable.com/). The the letter `x` for example, would be read as `120`, which is greater than `0` and thus pass your check. It's very possible I'm forgetting something, but off the top of my head I can't think of any way to do this without storing the input into a string first and then using `atoi()` to parse the string for a number (after checking it) – Salamosaurus Jun 29 '18 at 02:04