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 Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to check whether the string is number?

Status
Not open for further replies.
I try to convert a string into a number in a try/catch statement. If the catch statement is executed, the string is not valid. I prefer to use AnsiStrings since the conversion is easier but you can use C++ string, too. Just use the correct function.
Code:
String NumStr = "1234.56";
double MyNumber;
try
{
    MyNumber = StrToFloat(NumStr);
}
catch (...)
{
    String ErrMsg = NumStr + " is NOT a valid number!";
    Application->MessageBox(ErrMsg.c_str(), "Number Error!", mbOK);
}

The above code is a bit rough since I'm doing it from memory but you get then idea. You could also check for integers and dates the same way.


James P. Cottingham
-----------------------------------------
[sup]I'm number 1,229!
I'm number 1,229![/sup]
 
How to use this command inside my below command?i want to check whether text[1]to text[5] whether it is number?
Code:
#pragma hdrstop
#include <stdio.h>
#include <conio.h>
#include <string.h>
#pragma argsused

char **text;
int i;
char str[] = ": Goolie p q h k clearLeftState ;";
char *token;
char seps[] = " ";
int n;
int count;
int sign = 0;

void store()
{
   text[ n ] = new char[ strlen(token) + 1 ];
   strcpy( text[ n ], token );
   n++;
}
//-----------------------------------------------------------------------

int main(int argc, char* argv[])
{
  
   text = new char*[100];
   token = strtok(str,seps);
   while( token != NULL )
    {
        //printf( "%s\n", token );
        store();
        count++;
        token = strtok( NULL, seps );
    }

      for (n = 0 ; n < count; n++)
   {
        printf("text [%d] = %s\n", n, text[n]);
   }

Thank for Reply

Singapore Swimming Lessons
 
#include <cctype>
char ch = '2';
isdigit(ch);

isdigit() returns returns 1 if it's a number, 0 if it's a letter -1 if it failed. and you pass it a character
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top