Code:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <string.h>
#include <ctime>
using namespace std;
int main()
{
int input;
while (cout << "Enter a number: " && !(cin >> input))
{
cout << "\nInvalid input" <<endl;
cin.clear();
cin.ignore(std::numeric_limits < int >::max(), '\n');
}
cout <<"You entered " <<input;
return 0;
}
This code example works in Dev C++ but not in Visual C++.NET. The error messages I get are:
Splicer.cpp(41) : error C2039: 'numeric_limits' : is not a member of 'std'
Splicer.cpp(41) : error C2065: 'numeric_limits' : undeclared identifier
Splicer.cpp(41) : error C2062: type 'int' unexpected
In any case I need to be able to trap the error if a user enters an illegal value into an integer variable (such as 'a" or "hahaha") while preventing the program from spiraling into the killer infinite loop.