you are passing a string in the form of text.c_str() to isdigit() when isdigit expects to be passed just a single char.
You can use any reasonably large value for ignore() and probably get away with it. say cin.ignore(32000). The biggest number you can use, which means the safest value you can use, I believe is call INT_MAX and it is declared in limits.h if your compiler uses the old style header files and climits if it uses the new style header files.
You can try these miniprograms. I'm sure you can break either one. But you'll have to work at it a bit first, assuming I didn't leave any bugs in them.
Code:
#include <iostream>
#include <climits>
using namespace std;
int main()
{
int input;
bool valid = false;
while(!valid)
{
cou << "enter an int" << endl;
cin >> input;
if(cin.fail())
{
cin.clear();
cin.ignore(INT_MAX, '\n');
cout << "invalid input" << endl;
}
else
{
cout << input << " is valid" << endl;
valid = true;
}
}
return 0;
}
Code:
#include <iostream>
#include <cstdlib>
#include <cstring>
using namespace std;
int main()
{
char stringInput[256];
int input;
bool valid = false;
int length;
int i;
while(!valid)
{
cout << "enter an positive integer < 1000" << endl;
cin >> stringInput;
length = strlen(stringInput);
for(i = 0; i < length; ++i)
{
if(!isdigit(stringInput[i]))
i = 403;
}
if(i == length && length < 4)
{
valid = true;
input = atoi(stringInput);
}
else
cout << "invalid input." << endl;
}
cout << input << " is valid" << endl;
return 0;
}