Forget about ASCII code checks, use isdigit or isnumber (see man isnumber). The first function checks whether the character is 0–9, the second one also accepts various other number characters depending on the current locale.

There may even be better functions to do the check – the important lesson is that this is a bit more complex than it looks, because the precise definition of a “number string” depends on the particular locale and the string encoding.

Answer from zoul on Stack Overflow
🌐
Quora
quora.com › How-do-you-check-if-a-string-is-a-number-in-C-1
How to check if a string is a number in C - Quora
Answer (1 of 23): There’s a function that does exactly what you’re asking for [code]isdigit(c); [/code]isdigit(c) is a function in C which can be used to check if the passed character is a digit or not. It returns a non-zero value if it’s a digit else it returns 0. And by digit it’s anything fro...
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-if-a-c-cplusplus-string-is-an-int
How to check if a C/C++ string is an int?
Here is an example to check whether a string is an int or not in C++ language, ... #include<iostream> #include<string.h> using namespace std; int main() { char str[] = "3257fg"; for (int i = 0; i < strlen(str); i++) { if(isdigit(str[i])) cout<<"The string contains int\n"; else cout<<"The string does not contain int\n"; } return 0; }
🌐
Cprogramming
cboard.cprogramming.com › cplusplus-programming › 33859-how-can-i-determine-whether-string-represents-integer-not.html
How can I determine whether a string represents an integer or not?
Well, you could use atoi (Ascii TO Integer) or the like (atof, atol, etc) and determine if the result is a number. atoi returns 0 if the string was not a number. Possibly your string IS the number 0, so just for added safety, in this example we check to make sure that both the value is non-zero ...
🌐
GeeksforGeeks
geeksforgeeks.org › dsa › program-check-input-integer-string
Program to check if input is an integer or a string - GeeksforGeeks
public class Main { // Returns true if s is a number else false static boolean isNumber(String s) { for (int i = 0; i < s.length(); i++) { if (!Character.isDigit(s.charAt(i))) { return false; } } return true; } // Driver code public static void main(String[] args) { // Saving the input in a string String str = "6790"; // Function returns true if all characters // are in the range '0' - '9' if (isNumber(str)) { System.out.println("Integer"); } else { System.out.println("String"); } } } ... # Python 3 program to check if a given string # is a valid integer # This function returns True if s is a
Published   May 24, 2024
🌐
Island Class
islandclass.org › 2020 › 05 › 14 › checking-for-integers-c-code
Checking for Integers (in C code) – Island Class
May 15, 2020 - } } return 0; } bool isNumber(char* stringArray) { //go through each character //location in the array until //we reach the null character (end of input) for (int i = 0; stringArray[i]!='\000'; i++) { if(isdigit(stringArray[i])==0)//if the current character is not a digit.... return false; //return false and end function here } return true;//return true since the entire array contained numeric characters } ... The basic strategy for checking for integer input, is to read all input as text and use the atoi(…) function to try converting the input whilst performing error checking.
🌐
Reddit
reddit.com › r/cs50 › checking if a string is a integer and then converting it.
r/cs50 on Reddit: Checking if a string is a integer and then converting it.
February 26, 2023 -

I am at "Caesar" with the encryption code and I just need to turn the input into an integer and print an error messsage if it is not an integer. I can't seem to find a solution online including the right libraries for the Github cs50 VS (like iostream dont work?) and I can't find any clues in the lecture notes on this! Where do I go?

🌐
Medium
medium.com › @ryan_forrester_ › c-check-if-string-is-number-practical-guide-c7ba6db2febf
C++ Check if String is Number: Practical Guide | by ryan | Medium
January 7, 2025 - One of the simplest ways to check if a string is a number is by using `std::istringstream`. This method works well for basic cases and can handle both integers and floating-point numbers.
Find elsewhere
🌐
TutorialsPoint
tutorialspoint.com › how-to-check-if-an-input-is-an-integer-using-c-cplusplus
How to check if an input is an integer using C/C++?
#include <iostream> using namespace std; bool isNumeric(string str) { for (int i = 0; i < str.length(); i++) if (isdigit(str[i]) == false) return false; //when one non numeric value is found, return false return true; } int main() { string str; cout << "Enter a string: "; cin >> str; if (isNumeric(str)) cout << "This is a Number" << endl; else cout << "This is not a number"; }
🌐
YouTube
youtube.com › watch
Check if a String Is an Int Using C - YouTube
Check if a String Is an Int Using CGreetings, in this tutorial we shall be checking if a string (or character array) is a number (integer/int) in C. We can c...
Published   July 18, 2023
Views   139
🌐
Arduino Forum
forum.arduino.cc › projects › programming
check if string is numeric - Programming - Arduino Forum
January 8, 2014 - Can't get this to work.. String str = "+1.1234"; isnan(str.toFloat()) ; -> FALSE String str = "-------"; isnan(str.toFloat()) ; -> FALSE (shuold return TRUE, since it is NOT A number) Always returns FALSE, since str…
🌐
Quora
quora.com › How-do-you-check-for-integers-in-a-string-in-C
How to check for integers in a string in C - Quora
Answer (1 of 4): Question: In C, how would I check if the input string has an integer? I would determine this by trying to convert it to an integer. There are 3 functions that come to mind that may do the trick and one algorithm that is slightly more elaborate that will use a 4th function for it...
🌐
Raspberry Pi Forums
forums.raspberrypi.com › board index › programming › c/c++
C Newbie - How to Validate Integer Input - Raspberry Pi Forums
Other things to look out for using this method is the string having a trailling newline - which fgets() will read and store in the string, so you may have to check for this beforehand. -Gordon This really helped my understanding of sscanf. As for the trailing new lines, I used the following code before the sscanf. ... char *newline; if ((newline=strchr(text, '\n')) != NULL) *newline = '\0'; This seems to eliminate the trailing new line (which did cause a few headaches...). Anyway, I've got the thing to work, it certainly gets the Integers and removes any non-int characters.
Top answer
1 of 3
4

According with the man page of strtol. You must define your function such as:

bool isNumeric(const std::string& str) {
    char *end;
    long val = std::strtol(str.c_str(), &end, 10);
    if ((errno == ERANGE && (val == LONG_MAX || val == LONG_MIN)) || (errno != 0 && val == 0)) {
        //  if the converted value would fall out of the range of the result type.
        return false;   
    }
    if (end == str) {
       // No digits were found.
       return false;
    }
    // check if the string was fully processed.
    return *end == '\0';
}

In C++11, I prefer to use std::stol instead of std::strtol, such as:

bool isNumeric(const std::string& str) {
    try {
        size_t sz;
        std::stol(str, &sz);
        return sz == str.size();
    } catch (const std::invalid_argument&) {
        // if no conversion could be performed.
        return false;   
    } catch (const std::out_of_range&) {
        //  if the converted value would fall out of the range of the result type.
        return false;
    }
}

std::stol calls std::strtol, but you works directly with std::string and the code is simplified.

2 of 3
3

strtol stops on the first non digit

but if you read the man page http://man7.org/linux/man-pages/man3/strtol.3.html you can see

If endptr is not NULL, strtol() stores the address of the first invalid character in *endptr. If there were no digits at all, strtol() stores the original value of nptr in *endptr (and returns 0). In particular, if *nptr is not '\0' but **endptr is '\0' on return, the entire string is valid.

ie

string testString = "ANYTHING";
cout << "testString = " << testString << endl;
char *endptr;
int testInt = strtol(testString.c_str(),&endptr,0);
if(**endptr)
   cout << "bad input";
🌐
Tek-Tips
tek-tips.com › home › forums › software › programmers › languages
How to check if a string is an integer - C | Tek-Tips
January 23, 2005 - To achieve the latter, strtol() will not necessarily work, as the data could be any number of bytes in length, and the content may well exceed the value an integer (int) can store (INT_MAX or UINT_MAX, limits.h). Therefore, the validation method would be to walk through the array checking each byte using isdigit() (ctype.h) to determine if it is a numeric.