C++ Program to check whether user has entered integer data or not [DEVCPP/GCC]


STREAM RELATED FUNCTIONS

fail()


This function returns true whenever an error occurs in input operation.

clear()

This function overwrites the stream error state flags.

ignore()

It is used to discard characters from input stream.

PROGRAM

// Program to check whether user has entered integer data or not

#include <iostream>

using namespace std;

int main()
{
        int num ;
        char ch;

        do
{

cout<<"\n\nENTER NUMBER: ";
cin>>num ;

if(cin.fail())                                    // Checks For Undesired Input
{
cout<<"PLEASE ENTER A NUMBER !";
cin.clear();                                //  Overwrites the Error Flags
cin.ignore(INT_MAX , '\n');    //  Clears Input Stream
}
else
{
cout<<"YOU ENTERED "<<num;
}

cout<<"\nDO YOU WISH TO CONTINUE(Y/N): ";
cin>>ch;

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

return 0;
}

OUTPUT

C++ Program to check whether user has entered integer data or not with output

Share this

Related Posts

FIND US ON FACEBOOK!