I believe this does everything ZuK's version does, just a little more succinctly:Code:bool is_int(const std::string& s)
{
int i;
std::istringstream istr(s);
if (!(istr >> i))
return false;
return istr.rdbuf()->in_avail() == 0;
}
Printable View
I believe this does everything ZuK's version does, just a little more succinctly:Code:bool is_int(const std::string& s)
{
int i;
std::istringstream istr(s);
if (!(istr >> i))
return false;
return istr.rdbuf()->in_avail() == 0;
}
In C at least, a function name starting with "is" and followed by a lowercase letter is reserved (for future use in <ctype>). Name it is_int instead or something.Code:bool isint( const string& s ) {
None of the versions work with hex (as currently implemented). I believe the stringstream version would require using an io manipulator, which would require a lot more code since you don't know which manipulator to use.
you can't pass "0xfc" to atoi() and expect it to return the correct integer. Just ain't going to happen. Why stop at hex, why not pass octal too? That function isn't supposed to convert numbers from one base to another. Use the "KISS" (Keep It Simple Stupid) principal here.Quote:
Originally Posted by Stoned_Coder
atoi expects const char* not int
Yes why not do octal too. Anything the language sees as a valid int.
>> atoi expects const char* not int
"0xfc" is a const char*.
Even the C++ input mechanism doesn't deal with the issue of hex or octal. Your code has to specify that you want to read in the input in hexadecimal format from the user. Otherwise, using cin >> an_int will fail if the user types 0xff. Since the OP wants to know about validating user input, I think adding checks for other bases is probably not necessary.
I didn't mean pass it an int, a stringQuote:
Originally Posted by Stoned_Coder
atoi("0xff");
Of course there are standard library functions that will convert strings from one base to another, but it requires another parameter to tell it what base to use. I don't see that as the op's intention.Quote:
Originally Posted by Stoned_Coder
Dave: your solution is really great. ;) I didn't see it before posting my last response.
the op asked for a way to make sure input was a valid int. well input in hex or octal is also a valid int.
A func that takes an int param(not const char* as atoi does, i never said that would work), can be passed a hex or octal number and thats fine because it is an int.
So I believe any function designed to check an ints validity has to take octal and hex into account. Dont you?
I don't agree. The function takes a string, not an int. When you pass a hex (or oct), the compiler converts it to internal int format, which means you aren't passing a hex or oct at all. The receiving function never sees what you typed in the source code.
The above won't work because is_int() expects the parameter to be a string. What is the purpose of overloading is_int() to accept an int instead of a string? An int is obviously a valid int or it wouldn't be called an int. How can an int possibly be an invalid int?Code:is_int(0xff);
I don't thin kit is really necessary to check for hex and octal values, but if that is what you want, I think this is appropriate:And a test program:Code:#include <sstream>
#include <string>
bool is_int(const std::string& s)
{
int i;
std::istringstream istr(s);
istr.unsetf(std::ios::dec);
if (!(istr >> i))
return false;
return istr.rdbuf()->in_avail() == 0;
}
Code:#include <iostream>
int main()
{
if (is_int("1234"))
std::cout << "1234 is int." << std::endl;
else
std::cout << "1234 is not int." << std::endl;
if (is_int("12ab34"))
std::cout << "12ab34 is int." << std::endl;
else
std::cout << "12ab34 is not int." << std::endl;
if (is_int("2147483648"))
std::cout << "2147483648 is int." << std::endl;
else
std::cout << "2147483648 is not int." << std::endl;
if (is_int("2147483647"))
std::cout << "2147483647 is int." << std::endl;
else
std::cout << "2147483647 is not int." << std::endl;
if (is_int("-1234"))
std::cout << "-1234 is int." << std::endl;
else
std::cout << "-1234 is not int." << std::endl;
if (is_int("0x12ab34"))
std::cout << "0x12ab34 is int." << std::endl;
else
std::cout << "0x12ab34 is not int." << std::endl;
if (is_int("02222222222"))
std::cout << "02222222222 is int." << std::endl;
else
std::cout << "02222222222 is not int." << std::endl;
if (is_int("6."))
std::cout << "6. is int." << std::endl;
else
std::cout << "6. is not int." << std::endl;
}