How do I check if char contains int. I just need to be able to tell if char[256] contains a integer.
How do I check if char contains int. I just need to be able to tell if char[256] contains a integer.
You may find std::isdigit() from <cctype> useful.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
Do you want to check if the entire string is an int, or just check if any of the characters are integers?
If you want to see if any characters are integers, then loop through using isdigit.
However, if you want to see of the string holds a number, use a stringstream. Initialize the istringstream with the string, then attempt to read into an int variable. If the read fails then the string does not contain a valid int, if it succeeds then it does.
There are variations and additional checks you can do. For example, if the string is "123abc" then you will also want to make sure the streingstream is empty after reading into the int.