validate a number?

Posted on 2002-05-01
Last Modified: 2006-11-17
Hello all

I need a function that can tell if a number is, in fact, a number.
isdigit() does it for one char, but what about, ie, 25?

The function needs to be vanilla standard STL C++.


Question by:vlg
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
LVL 30

Expert Comment

ID: 6984800
Do isdigit(char[0]).

const char* SomeNum = "25";

if (isdigit(SomeNum[0])) printf("It's a number");
LVL 30

Accepted Solution

Axter earned 50 total points
ID: 6984823
If you want to check the full string for digits, you can use the following function.

bool IsStringDigit(const char* Src)
     int len = strlen(Src);
     if (!len) return false;
     for(int x = 0;x < len;++x)
          if (!isdigit(Src[x])) return false;
     return true;
LVL 86

Expert Comment

ID: 6984847
You could also use wht the CRT offers - the 'strtoXX()' functions have a built-in check for number formats, so if you are dealing with ints, e.g.

#include <stdlib.h>

int n;
char* psz = "253";
char* stop;

n = (int) strtol ( psz, &stop, 10);

if ( *stop) {

 // if *stop is non-null, an invalid char was encountered


Author Comment

ID: 6984871
thanks everyone for their answers!
LVL 30

Expert Comment

ID: 6984874
You still have to check for he string length using the strtol method.

For example, with out checking for strlen, the following code would incorrectly interpret psz_bad2 as a number.

int n;
char* psz = "123";
char* psz_bad = "253x";
char* psz_bad2 = "";
char* stop;

n = (int) strtol ( psz, &stop, 10);
if (!stop[0]) printf("Is a number\n");
else printf("Not a number (%s)\n", stop);

n = (int) strtol ( psz_bad, &stop, 10);
if (!stop[0]) printf("Is a number\n");
else printf("Not a number (%s)\n", stop);

n = (int) strtol ( psz_bad2, &stop, 10);
if (!stop[0]) printf("Is a number\n");
else printf("Not a number (%s)\n", stop);


Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

724 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question