String Input Validation

Good day!

May I know please how can i check or validate if my String input is an integer or a floating point number? Thank you so much in advance!
closed account (Dy7SLyTq)
<cctypes>
cctype only returns in value... what Im trying to do is that i will input a string lets say "123" then the program will say "it is an integer" then it will convert to integer... If I will input "1.44" then it will say "it is a float" then it will convert it to "float" (for arithmetic operation)
closed account (Dy7SLyTq)
you can do that with cctypes but whatever... if you want to write the code yourself then you can just use a regex library and then test for ints and floats
could please give me a sample snippet of code? Thanks! :)
closed account (Dy7SLyTq)
what compiler do you use?
Im using g++ on ubuntu 13...
closed account (Dy7SLyTq)
damn... then you have to use boost
1
2
3
4
5
6
7
8
9
10
11
12
double IsFloat(std::string Buffer)
{
	if(some_regex_match("[0-9]+\.[0-9]+", Buffer))
	{
		std::istringstream Stream(Buffer);
		double TempFloat;
		Stream >> TempFloat;
		return TempFloat;
	}

	return 0.00000000000000000000000001 //i cant think of a better way to say it wasnt a float
}


then for the int just replace every instance of double with int, the [0-9]+\.[0-9]+ with [0-9]+ and return something other than 0.000...01
> you can do that with cctypes but whatever... if you want to write the code yourself
I wonder what the hell are you talking about.


http://www.cplusplus.com/forum/beginner/13044/#msg62827
basically, try to read as integer, if it was successful then it was an integer.
then try with float.


> i cant think of a better way to say it wasnt a float
bool is_float(std::string)

your function may be better called `string_to_float'
in which in case of failure you could have returned 0, returned <0, returned `NaN', setting a global variable (errno), throwing an exception, returned a duple with a bool, or a boost optional
Last edited on
closed account (Dy7SLyTq)
I wonder what the hell are you talking about.
i could do this with ctypes.h.
in which in case of failure you could have returned 0, returned <0, returned `NaN', setting a global variable (errno), throwing an exception, returned a duple with a bool, or a boost optional
good point. like i said i couldnt think of stuff at the moment
the functions in <cctype> work per character, so you'll have to write your parse function (which should take into account invalid constructions with valid characters)
However your reply seems to imply that it would be a trivial task like simply calling the `is_float()' function.

you forgot the minus sign in your regex
and scientific notation
Last edited on
> how can i check or validate if my String input is an integer or a floating point number?

By trying to interpret it as a number.

C++11:

integer: std::stoi() and friends http://en.cppreference.com/w/cpp/string/basic_string/stol
floating point: std::stod() and friends. http://en.cppreference.com/w/cpp/string/basic_string/stof

Reports errors by throwing exceptions; an output parameter holds the position of the first unconverted character.


C++98:

integer: std::strtol() http://en.cppreference.com/w/cpp/string/byte/strtol
floating point: std::strtod() http://en.cppreference.com/w/cpp/string/byte/strtof

Reports errors by return value and errno; an output parameter holds the address of the first unconverted character.


> you forgot the minus sign in your regex
> and scientific notation

And a whole lot more. http://en.cppreference.com/w/cpp/string/basic_string/stof


> could please give me a sample snippet of code?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <string>

bool is_double( const std::string& str )
{
    std::size_t pos = 0 ;

    try { std::stod( str, &pos ) ; }
    catch(...) { return false ; }

    for( auto i = pos ; i < str.size() ; ++i )
        if( !std::isspace( /*str[pos]*/ str[i] ) ) return false ;
        // EDIT: corrected by abhishekm71

    return true ;
}

int main()
{
    for( std::string str : { "12", "12.", "-12.e+23", "+0xabc.dP-5", "-Infinity", "NaN", "inf", "nan", // all true
                             "-12.e+0x23", "vjvj", "12.34.56", "12.e" } ) // all false
    {
        std::cout << str << " => " << std::boolalpha << is_double(str) << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/0788182ab814e1ac
Last edited on
@JLBorges

Sorry I don't understand:
if( !std::isspace( str[pos] ) )

Shouldn't it be
if( !std::isspace( str[i] ) )

As far as I have understood you are checking that there are no more non-space characters remaining in the string. Right?
> Shouldn't it be if( !std::isspace( str[i] ) )
> you are checking that there are no more non-space characters remaining in the string. Right?

Yes. And yes. Thank you!
Topic archived. No new replies allowed.