你如何在C ++中进行整数验证?

时间:2016-03-14 22:34:54

标签: c++

好吧,我一直在寻找几天,但我找不到任何可行的方法。

我有一个程序,我想确保用户输入一个整数而不是双精度。

这个程序工作正常但我需要验证numOne和numTwo以确保它们是整数而不是双精度,(5.5)

            int main()
        {       //This is where my variables are stored
                int numOne, numTwo, answer, rightAnswer, ranNumOne, ranNumTwo;


                //this will display to the user to enter a range of numbers to be used
                cout << "Please enter a set of numbers to be the range for the problems." << endl;
                cout << "Please enter the beginning number." << endl;
                cin >> numOne;      
                cout << "please enter the ending number." << endl;
                cin >> numTwo;

                //this makes sure that the user entered a integer(if not the program will close)
                if (!(cin >> numOne)) 
                {
                    cout << "You did not enter a integer PLEASE RE-RUN THE PROGRAM AND TRY AGAIN!" << endl;
                    cin.clear();    
                    cin.ignore(100, '\n');
                    exit(0);
                }

                cout << "please enter the ending number." << endl;
                cin >> numTwo;

                //this makes sure that the user entered a number(if not the program will close)
                if (!(cin >> numTwo)) 
                {
                    cout << "You did not enter a integer PLEASE RE-RUN THE PROGRAM AND TRY AGAIN!" << endl;
                    cin.clear();    
                    cin.ignore(100, '\n');
                    exit(0);

                }

                //this is where the first number is generated       
                srand(time(0));
                ranNumOne = rand() % (numOne - numTwo) + 1;
                system("PAUSE");  

                //this is where the second number is generated
                srand(time(0));
                ranNumTwo = rand() % (numOne - numTwo) + 1; 

                //this is where the calculations are done
                rightAnswer = ranNumOne + ranNumTwo;

                //this displays the problem that was generated
                cout << "What is: " << endl;        
                cout << setw(11) << ranNumOne <<  endl;
                cout << setw(6) << "+" << setw(3) << ranNumTwo <<  endl;
                cout << "     -------\n";
                cin >> answer;

                //this checks to see if the answer is right or not and displays the result
                if (answer == rightAnswer)
                {
                    cout << "Your answer was correct! " << endl;            
                }
                else
                cout << "The correct answer is: " << rightAnswer << endl;



            return 0;
        }

2 个答案:

答案 0 :(得分:0)

使用std:n:ci.fail()查看是否失败。

int numOne;
cin >> numOne;
if(cin.fail())
    cout << "Not a number...")

甚至可能是一个很好的模板功能。

template<typename T>
T inline input(const std::string &errmsg = "") {
    T var;
    std::cin >> var;
    while (std::cin.fail()) {
        std::cin.clear();
        std::cin.ignore(256, '\n');
        std::cout << errmsg;
        std::cin >> var;
    }
    return var;
}

或不:

#include <iostream>
#include <cstdlib>
#include <iomanip>
#include <string>
#include <ctime>

#define DIFF(n1, n2) (n1 > n2 ? n1 - n2 : n2 - n1)

using namespace std;


int input(const string &firstmsg = "", const string &errmsg = "") {
    int var;
    std::cout << firstmsg;
    std::cin >> var;
    while (cin.fail()) {
        cin.clear();
        cin.ignore(256, '\n');
        cout << errmsg;
        cin >> var;
    }
    return var;
}


int main(){
    //This is where my variables are stored
    int numOne, numTwo, answer, rightAnswer, ranNumOne, ranNumTwo;


    //this will display to the user to enter a range of numbers to be used
    cout << "Please enter a set of numbers to be the range for the problems." << endl << endl;
    numOne = input("Please enter the beginning number: ", "Invalid. Enter again: ");

    //this asks the user for the second number
    numTwo = input("Please enter the ending number: ", "Invalid. Enter again: ");

    //this is where the first number is generated       
    srand(time(0));
    ranNumOne = rand() % (DIFF(numOne, numTwo)) + 1; // ensures it will always be positive
    system("PAUSE");

    //this is where the second number is generated
    srand(time(0));
    ranNumTwo = rand() % (DIFF(numOne, numTwo)) + 1;

    //this is where the calculations are done
    rightAnswer = ranNumOne + ranNumTwo;

    //this displays the problem that was generated
    cout << "What is: " << endl;
    cout << setw(11) << ranNumOne << endl;
    cout << setw(6) << "+" << setw(3) << ranNumTwo << endl;
    cout << "     -------\n";
    cin >> answer;

    //this checks to see if the answer is right or not and displays the result
    if (answer == rightAnswer){
        cout << "Your answer was correct! " << endl;
    }
    else
        cout << "The correct answer is: " << rightAnswer << endl;
    return 0;
}

答案 1 :(得分:0)

为什么不,将数字转换为double,然后查看该double是否为int。即

double d;
cin>>d;
if (ceil(d) != d)
 cout >> " not an integer";