Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to 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 3-Mar-13 20:25pm
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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:
 
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:
 

#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;
}
  Permalink  
v2
Comments
Vaibhav_J_Jaiswal at 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 at 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);
du[DE] at 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 ...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

These are standard template solutions:
 
#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;
}
 
// 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!
  Permalink  
Comments
David Serrano Martínez at 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)

  Print Answers RSS
0 OriginalGriff 5,130
1 DamithSL 4,237
2 Maciej Los 3,700
3 Kornfeld Eliyahu Peter 3,470
4 Sergey Alexandrovich Kryukov 2,846


Advertise | Privacy | Mobile
Web03 | 2.8.141216.1 | Last Updated 4 Mar 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100