Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gmmastros on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Testing if a string contains a numeric value?

Status
Not open for further replies.

VBDotNetProgrammer

Programmer
Jul 9, 2003
50
0
0
GB
Hi,

I want to be able to test if a string is numeric. Kind of like the isNumeric() function in VB6.

Has anyone got any ideas how to go about this? Ive tried using isdigit() and isalnum() but both require an int as a parameter and i want to test a string.

Thanks alot

PS It has to be portable as well.
 
Code:
#include <string>
#include <cctype>
string s;
...
...isdigit(s[i])... 
// i-th character will be promoted to int...
It works fine. What's a problem?

 
The isdigit function returns true if a single digit is a number, so you have to test each character in the entire string (and handle stuff like decimal points).

Another way to do it is to use a stream to try to put the string into a numeric variable:
Code:
#include <string>
#include <sstream>

int main()
{
  std::string test("1234.56");
  std::istringstream inpStream(test);
  double inpValue = 0.0;
  if (inpStream >> inpValue)
  {
    // ... Success!!  test is a number.
  }
  else
  {
    // ... Failure!!  test is not a number.
  }
}
 
It's freeware (in public domain;):
Code:
#include <iostream>
// iostream for test only...
#include <string>
#include <cstring>

using namespace std;

namespace {
	const char* spaces = " \t";
	const char* digits = "0123456789";
}

int IntLen(const char* cstr)
{
  int	k, n = 0;
  if (cstr)
  {
	n = strspn(cstr,spaces);
	cstr += n;
	if (*cstr == '-' || *cstr == '+')
		++cstr, ++n;
	k = strspn(cstr,digits);
	n = k?n+k:0;
  }
  return n;
}
/// <int>::[spaces][+|-]<digits>[garbage]
inline bool isInt(const char* cstr)
{
  return IntLen(cstr) > 0;
}

inline bool isInt(const string& s)
{
  return isInt(s.c_str());
}

/// <number>::[spaces]<int>[e|E[+|-]<uint>][garbage]
bool isNumber(const char* cstr)
{
  int	n = IntLen(cstr);
  if (n)
  {
	cstr += n;
	if (*cstr == 'e' || *cstr == 'E')
	   n = strspn(++cstr,digits);
  }
  return n > 0;
}

inline bool isNumber(const string& s)
{
  return isNumber(s.c_str());
}

inline void Test(const string& s)
{
  cout << isNumber(s) << endl;
}

int main()
{
  char line[80];
  while (cout << "Number:",
         cin.getline(line,sizeof line,'\n')
  ) Test(line);
  return 0;
}
 
just do a strspn(number,"0123456789");

if the return is the same length of number, then you have all digits.
 
To cdlvj:
how about +12345 or -67890?..
 
There is an ASCII value associated with each character. So you can check if the ascii value

Let's say the string is stored in variable STRING.

func isContainingOnlyDigits(char *string)
{
for (i=0;i<strlen(STRING);i++)
{
if( STRING>15 && STRING<26)
{
}
else
return false;


return true;
}
}

You can better on the code, I just wanted to give you an idea. Make your function. Small and Simple.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top