hi..
i was wondering if anyone knew a c++ substitute for the php function is_int: http://www.php.net/is_int
please reply if you do (and also with which headers i need to include)
thanks!
laney :/
hi..
i was wondering if anyone knew a c++ substitute for the php function is_int: http://www.php.net/is_int
please reply if you do (and also with which headers i need to include)
thanks!
laney :/
isdigit() is the closest standard function, in <cctype> or <ctype.h>.
ok maybe you need to see my code then..
i declare the variable like this:
int range;
and then i get input from a cin:
cout<<"Enter number range (1-?): ";
cin>>range;
(this is in a loop.. the loop is here (to stop a user entering 0))
do
{
cout<<"Range must be greater than 0.\r\n";
cout<<"Enter number range (1-?): ";
cin>>range;
} while (range <= 0);
the problem is if a user enters something that isn't a number then it goes into an infinite loop of the two couts above... i guessed something like is_int would fix it but i can't seem to find anything :/
Try something like -
int i;
cin >> i;
while(cin.fail())
{
cin.clear();
cin.ignore(1);
cin >> i;
}
ohh works thanks
Code:do { cout<<"Range must be greater than 0.\r\n"; cout<<"Enter number range (1-?): "; cin>>range; while(cin.fail()) { cin.clear(); cin.ignore(1); range = 0; } } while (range <= 0);