如何简单地在C ++中正确验证?

时间:2018-06-28 03:22:22

标签: c++

我是c ++的新手,我无法验证用户输入的内容必须是整数,而我只想了解基本的简单方法。 (只是基本方法)验证用户的输入,如何使其在下面的代码之前起作用。谢谢您的帮助和时间。例如:如果我得到提示输入正数并输入字母x,则它将显示“无效输入”。谢谢!

这是我的代码:

已更新

     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;

}

1 个答案:

答案 0 :(得分:1)

您可以从cin中读取一个字符串,并检查每个字符以确保在传递给isdigit()时它返回true。

std::string x;
std::cin >> x;
for(char c : x){ //for each char c in string x
   if(!isdigit(c)) //Invalid
}