Solved

# validate a number?

Posted on 2002-05-01
303 Views
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++.

Thanks

Edmund
0
Question by:vlg
[X]
###### 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
• 3

LVL 30

Expert Comment

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

Example:

0

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;
}
0

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
}

0

Author Comment

ID: 6984871
0

LVL 30

Expert Comment

ID: 6984874
FYI:
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* 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);

0

## Featured Post

Question has a verified solution.

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

Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.
###### Suggested Courses
Course of the Month7 days, 7 hours left to enroll