How to make cin take only numbers

后端 未结 4 1100
青春惊慌失措
青春惊慌失措 2020-11-22 07:30

Here is the code

double enter_number()
{
  double number;
  while(1)
  {

        cin>>number;
        if(cin.fail())
        {
            cin.clear()         


        
4条回答
  •  死守一世寂寞
    2020-11-22 07:57

    #include
    #include
    #include
    using namespace std;
    int get_int(void);
    int main()
    {
        puts("Enter a number");
        cout<<"The integer is "<

    This works fine for any integer. It can even check if you enter a space or any other character after the integer. The only problem is that it does not make use of std::cin. But, the problem with std::cin is that it ignores any space character after the integer and happily takes the integer as input.

提交回复
热议问题