I have built a very simple Parse class to detect whether a users input can be input into an integer variable or not. Here is the code:
This code seems WAY too simple though, but it seems to work just fine... Am I missing something here or is this just how simple it should be?Code:#include <iostream>
using namespace std;
class inputParser
{
public:
bool IntParse(istream& _stream1, int& _int1)
{
_stream1 >> _int1;
if (_stream1.fail() || _stream1.bad())
return false;
else
return true;
}
};
int main()
{
int someInt;
inputParser Input;
cout << "Input an integer: ";
if (Input.IntParse(cin, someInt))
cout << "Is an integer";
else
cout << "Is NOT an integer";
system("pause");
return 0;
}
