[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

validate a number?

Posted on 2002-05-01
5
Medium Priority
?
328 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
[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
  • Learn & ask questions
  • 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.
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.

650 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