Click here to Skip to main content
15,890,897 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi everybody,

I am new in vc environment. Is some one suggest me how I validate the numeric string?

My requirements are as follows:
a)numeric string have negative numbers.
b)range is from -1000 to 1000.

Currently I do the following things:
a)remove leading zeros.
b)remove special symbols and alphabets.
c)restrict the string only for four characters.

User enters combinations of any type like :
a)-1-0
b)1-00
c)--00
and so on.

I am not understanding how to restrict the above one.
Please anyone suggest me regarding to this things.
Posted

There are built-in functions like atoi, atof, atol and so long.
By using this function you don't need to care of restictions: the given string will be converted or you get zero back!

Take a look at e.g. atoi:

XML
function
atoi
<cstdlib>

int atoi (const char * str);

Convert string to integer
Parses the C-string str interpreting its content as an integral number, which is returned as a value of type int.


For example:


XML
#include <stdio.h>      /* printf, fgets */
#include <stdlib.h>     /* atoi */

int main ()
{
  int i;
  char buffer[256];
  printf ("Enter a number: ");
  fgets (buffer, 256, stdin);
  i = atoi (buffer);
  printf ("The value entered is %d. Its double is %d.\n",i,i*2);
  return 0;
}
 
Share this answer
 
v2
Comments
Vaibhav_J_Jaiswal 4-Mar-13 2:25am    
I used this one but when i enter the number as '2-2-', it return 2 which is illegal. So it is not useful for me. Any suggestions for this.
Jochen Arndt 4-Mar-13 3:10am    
Use strtol() and check which character stops the conversion:
char *pStop;
long n = strtol(buffer, &pStop, 10);
if (*pStop != '\0')
printf("%s is not a valid number\n", buffer);
Leo Chapiro 4-Mar-13 3:41am    
The question is, what is your approach: to get the number contained in a string or to parse the string by finding out whether it is correct number.
For the 1. approach take atoi or strtol like Jochen has written.
For the 2. one you can parse the string using regular expressions for example ...
These are standard template solutions:

C++
#include <string>
#include <sstream>
#include <stdexcept>

// Checks if a std::string can be converted to a type T.
template <typename T> bool Is(const std::string& s)
{
    T value;
    std::istringstream is(s);
    is >> value;

    if(!(is && (is >> std::ws).eof()))
        return false;

    return true;
}


C++
// Converts a std::string into a type T.
// (A std::invalid_argument exception is thrown if the conversion is not possible).
template <typename t=""> const T To(const std::string& s)
{
    T value;
    std::istringstream is(s);
    is >> value;
 
    if(!(is && (is >> std::ws).eof()))
        throw std::invalid_argument("Function: To<t>. Unallowed conversion.");
 
    return value;
}
</t></typename>


No restrictions are done, so you should apply restrictions after conversion.
I am afraid this does not work with strange inputs as you have pointed out. (Works with "-00" but not with the others).

I hope this sheds some light.

Best regards!
 
Share this answer
 
Comments
David Serrano Martínez 4-Mar-13 6:46am    
Where it reads t="", it must read T. Unable (twice) to write a well formatted answer :-(
Delete and other possible garbage.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900