Solved

validate a number?

Posted on 2002-05-01
5
267 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 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

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

743 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now