[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

validate a number?

Posted on 2002-05-01
5
Medium Priority
?
332 Views
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++.

Thanks


Edmund
0
Comment
Question by:vlg
  • 3
5 Comments
 
LVL 30

Expert Comment

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

Example:
const char* SomeNum = "25";

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

Accepted Solution

by:
Axter earned 200 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

by:jkr
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

by:vlg
ID: 6984871
thanks everyone for their answers!
0
 
LVL 30

Expert Comment

by:Axter
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* 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);

0

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
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…
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 use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

834 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