• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
Chad Smith

C++ String to a number

22 posts in this topic

I was working on my own function that would convert a string to a number, be it int, float, double or any of those built in data types.

 

Anyway writing the function is very easy and I do have it working properly.  My Question though is more a long the lines of dealing with when the string is not a number.  Currently I just build a stringstream on the string entered.  I then check to see if I can extract the data in the string stream into the number.  If not I just set it to 0 and return the number.  This works and if I enter a non number it returns 0.  Though is their a better way of doing this?  

 

I ask this because since 0 could easily be a valid number I wouldn't ever know if it was a valid 0 or if returned 0 based on the fact that it was an error.  How would y'all do this?  Just have it return 0?

 

I did also think about passing in the number with the string by reference and returning a boolean value if it could do the conversion.

 

Thanks if someone could explain the "best" or "correct" way of doing this and checking the conversion.

0

Share this post


Link to post
Share on other sites
Some other options: return a struct, pair or tuple that contains both an error code and the result of conversion; return the conversion but throw an exception on bad input; just use boost::lexical_cast instead and so on.

For error checking you might also want to check the situation where the first part of the string is a valid number but the rest isn't. Ex: "100fred"
1

Share this post


Link to post
Share on other sites

Thanks.  As soon as I posted this I remembered the C++11 functions so I went to the documentation to see about what it would return if the conversion wasn't possible.  I would more than likely use the C++11 in a true project, though I am writing some helper functions to just help me with some university projects.  The submission system compiler doesn't support a lot of C++11 right now it seems.  I will more than likely just throw an exception if the conversion could not be preformed.

 

Thanks SiCrane on the suggestion for checking if the rest of the string is a valid number also.  I actually did write a helper function already that loops through every character of the string to see if it is a valid char for a number (0-9, negative sign, or a decimal).  Also, just for learning and getting better: is their a better way to do it?  It would just seem on computers these days since a numerical string more than likely wouldn't be very long that it wouldn't take long to just loop through every character and checking each character while they are still valid numerical characters.

Edited by Chad Smith
0

Share this post


Link to post
Share on other sites
Since you're using a stringstream you can just use rdbuf()->in_avail() after you extract the number. If in_avail() returns true, then there's stuff left in the buffer, which means that part of it didn't parse as a number. This assumes that you've already stripped out the whitespace anyways.
2

Share this post


Link to post
Share on other sites
throw is a keyword, it doesn't live in namespace std.
0

Share this post


Link to post
Share on other sites

if you don't have c++11, no problem, you can use the atoi function.

 

It takes a c string as parameter returns an int, if you have a std::string just do

 

int myInt = atoi( myString.c_str() );

 

http://en.cppreference.com/w/cpp/string/byte/atoi

Yea I know, thanks though.  it's for both University Projects and to teach myself.  It had been a while since I've worked exclusively with C++ so I wanted to try to get myself used to working with std::stringstreams again.

 

Since you're using a stringstream you can just use rdbuf()->in_avail() after you extract the number. If in_avail() returns true, then there's stuff left in the buffer, which means that part of it didn't parse as a number. This assumes that you've already stripped out the whitespace anyways.

Thanks!  I'll have to do some quick research on that function!  I'll look into it now!

 

Thanks everyone!  Just wanted to learn some other and/or better ways to do some quick things.  Thanks for the suggestions!

Edited by Chad Smith
0

Share this post


Link to post
Share on other sites

Sorry I was thinking of throw std::bad_alloc

Why would you throw std::bad_alloc, the exception meant for memory allocation failure, for not being able to parse a string?
2

Share this post


Link to post
Share on other sites

I think he just means throw an exception in general. smile.png

 

If one was to be thrown, an std::runtime_error would be better. Perhaps a custom 'my::bad_conversion' or 'my::bad_parse' exception used by all your conversion functions, that inherits from std::runtime_error.

 

However, passing in non-conforming data into StringToX type functions aren't really exceptional behavior, IMO - but I suppose that depends on the project.

1

Share this post


Link to post
Share on other sites

EDITED DUE TOO THE COMMENT BELOW FROM MODERATION:

ABOUT THE PROGRAM' MEMORY LEAK; (FIX)

 

did you try checking the string[Lvalue] characters by using...

 

<ctype.h>

 

int _RTLENTRY _EXPFUNC isalnum (int __c);
int _RTLENTRY _EXPFUNC isalpha (int __c);
int _RTLENTRY _EXPFUNC iscntrl (int __c);
int _RTLENTRY _EXPFUNC isdigit (int __c);
int _RTLENTRY _EXPFUNC isgraph (int __c);
int _RTLENTRY _EXPFUNC islower (int __c);
int _RTLENTRY _EXPFUNC isprint (int __c);
int _RTLENTRY _EXPFUNC ispunct (int __c);
int _RTLENTRY _EXPFUNC isspace (int __c);
int _RTLENTRY _EXPFUNC isupper (int __c);
int _RTLENTRY _EXPFUNC isxdigit(int __c);
int _RTLENTRY _EXPFUNC isascii (int __c);

 

you could use these to maybe determine if you have a number or not.

you could also try being more specific to handling the errors by using a pointer return value

 

such as

typedef <type> number_;

 

//(EDIT)/////////////////////////////////////////////////////////////////

// pointer rtn is givin a static address only 1 time so there is no memory leaks'

//

number_ * stringtonum(string_type  *)

{

static number_ * rtn;                               

if( rtn == NULL){ rtn = new number_[2]; }

 

//where rtn[0] == error handle , rtn[1] == converted num

//.........do conversion block ..............///

 

return rtn;  //return address to variable rtn

}

 

 

example impliment:

 

#define out<<        ....

#define error_       ....

 

if(   (*(stringtonum(data)+0)) ) out<<error_; //is there an error in rtn?

 else

assign = (*(stringtonum(data)+1));

 

 

 

///edited to ocommidate  the BELOW COMMENT

///there is no leak if the return type is givin a static address -> to point too

Edited by Mathimetric
0

Share this post


Link to post
Share on other sites
That's really a horrible idea. If you're going to return a pair for error code and conversion result, then don't use dynamic allocation for it. Your own sample code leaks memory from using this kind of interface and calls the function twice to see if there's a conversion error.
0

Share this post


Link to post
Share on other sites

You can also use String-Streams!

 

Cheers smile.png!

I already am to do the conversion?  I thought I mentioned that in my first post.

If you just do the conversion with a stringstream and extract the number out of the string that isn't a pure number then the number will be filled with garbage

 

Quick example: if the the string we tried to convert was "abc20" then the number will be filled with garbage.  To me that then would need to require a check to make sure the conversion is possible at first so the number isn't filled with garbage.

 

Also I really do agree with Servant in that a "StringToX" function really isn't exceptional behavior.  Though it seems most of the library functions I see that do this throw an exception.  I do have it working by just throwing an exception if it couldn't do the conversion.  I then wrote another quick that just did the conversion and in a program would only do the conversion if the string was a number:

 

std::string myNumber = "20 abc";
int number = 0;

if(IsNumber(myNumber))
     number = ToNumber<int>(myNumber);


     

that way the conversion only happens if I know it is a number.  Though while this may work just fine in my own little projects for a university, I don't think this is very safe for other users.  It seems it would force the users to always check before they tried the conversion and would require the users to create their own way of dealing with and error like that.

 

Though maybe I'm over thinking it?

0

Share this post


Link to post
Share on other sites

std::string myNumber = "20 abc";
int number = 0;

if(IsNumber(myNumber))
     number = ToNumber<int>(myNumber);

that way the conversion only happens if I know it is a number.  Though while this may work just fine in my own little projects for a university, I don't think this is very safe for other users.  It seems it would force the users to always check before they tried the conversion and would require the users to create their own way of dealing with and error like that.

 

Not to mention that for every conversion, you have to iterate over the string twice instead of validating and converting at the same time (Schlemiel the Painter's algorithm, almost).

0

Share this post


Link to post
Share on other sites

T ConvertNumber(string numberStringToConvert, bool &wasANumber)

 

Return your number however you return it, and set your boolean to true if it was a number.

0

Share this post


Link to post
Share on other sites

 

std::string myNumber = "20 abc";
int number = 0;

if(IsNumber(myNumber))
     number = ToNumber<int>(myNumber);

that way the conversion only happens if I know it is a number.  Though while this may work just fine in my own little projects for a university, I don't think this is very safe for other users.  It seems it would force the users to always check before they tried the conversion and would require the users to create their own way of dealing with and error like that.

 

Not to mention that for every conversion, you have to iterate over the string twice instead of validating and converting at the same time (Schlemiel the Painter's algorithm, almost).

Even though I didn't know a name for it I was thinking that it would have to iterate over the string twice.

 

T ConvertNumber(string numberStringToConvert, bool &wasANumber)

 

Return your number however you return it, and set your boolean to true if it was a number.

That's a solution that I really didn't think of.  Thanks for listing another solution.

 

 

Ok, i just wanted to post another solution I did.  What would you change?  What should be better?  I know it isn't nearly the best solution.

IsNumber Function

bool IsNumber(const std::string& text)
{
	// const iterator to iterate through the string
	std::string::const_iterator it = text.begin();

	std::locale loc;

	// while we have not reached the end of the string and the character we are on is a digit
	// negative sign, or decimal point
	while(it != text.end() && (std::isdigit(*it, loc) || *it == '-' || *it == '.'))
	{
		it++;
	}

	return !text.empty() && it == text.end();
}

 

ToNumber

// ToNumber
// Takes in a string to convert to a number
// Checks to make sure that the string is a number before converting
// sets validNumber to true if it can convert, false otherwise
// Return Type Element to convert the string to any number
template <typename Element>
Element ToNumber(const std::string& text, bool& validNumber)
{
	Element number = 0;

	// If the string is a number, validNumber is true, otherwise false
	(IsNumber(text)) ? (validNumber = true) : (validNumber = false);
	
	// Only do the conversion if we have a validNumber;
	if(validNumber)
	{
		// istringstream buffer to hold the string passed in
		std::istringstream buffer(text);
		// extract the number from the buffer
		buffer >> number;
	}

	return number;
}

I still feel it could be a lot better.  I am still not a fan of the IsNumber Function.  I feel it could be a bit too confusing to follow for something that is so simple.

 

So couple questions:

1: Better solution?  As in am I still over thinking this?

 

2: is it right to use the c++ std::isdigit function here that takes the std::locale?  I saw an example using it and after trying to read about the std::locale I wasn't 100% sure what I was doing exactly.

 

3: Bad use of the Conditional Ternary Operator?  I know what it is saying just fine and I felt a full if/else statement was too long here for something really simple.  Though would this be by chance considered abusing the operator? 

Maybe I just need to get over always using the full if/else with the curly braces on something that simple (I for some reason do a double take every time the curly braces aren't used on those one liners if/else statements, yet I read the conditional operator just fine almost always).

Edited by Chad Smith
0

Share this post


Link to post
Share on other sites
First basic problem is that your IsNumber() function works the same for every single number type, but not every number type has the same valid format. What's a valid float is not necessarily a valid int. Secondly, you're only looking at valid characters without looking at the pattern of characters. IsNumber() would return true for "....." but this isn't a valid number for any data type. Next, if you're going to bring in a locale, then you should be looking at valid number formats for the given locales. For instance "10,000" is a valid formatted integer with a thousands separator for en-US, but the same number number formatted for de-DE is "10.000" neither of which would be parsed by std::stringstream properly as an integer. IsNumber() also misses the exponent portion of a float which generally starts with an "e" or "E". Also being a properly formatted number doesn't mean that the conversion would succeed. For instance, what happens when you want to format "10000000000000000000000000000" into a 32-bit int?

The ternary operator can be replaced very simply with [tt]validNumber = IsNumber(text);[/tt].

But again, the whole IsNumber() logic is simply duplicating the work std::stringstream already does when parsing the number itself. It would be less work, both in terms of programmer effort and in clock cycles at run time, and less bug prone to just to check the state of the stream after extraction.
1

Share this post


Link to post
Share on other sites

First basic problem is that your IsNumber() function works the same for every single number type, but not every number type has the same valid format. What's a valid float is not necessarily a valid int. Secondly, you're only looking at valid characters without looking at the pattern of characters. IsNumber() would return true for "....." but this isn't a valid number for any data type. Next, if you're going to bring in a locale, then you should be looking at valid number formats for the given locales. For instance "10,000" is a valid formatted integer with a thousands separator for en-US, but the same number number formatted for de-DE is "10.000" neither of which would be parsed by std::stringstream properly as an integer. IsNumber() also misses the exponent portion of a float which generally starts with an "e" or "E". Also being a properly formatted number doesn't mean that the conversion would succeed. For instance, what happens when you want to format "10000000000000000000000000000" into a 32-bit int?

The ternary operator can be replaced very simply with [tt]validNumber = IsNumber(text);[/tt].

But again, the whole IsNumber() logic is simply duplicating the work std::stringstream already does when parsing the number itself. It would be less work, both in terms of programmer effort and in clock cycles at run time, and less bug prone to just to check the state of the stream after extraction.

 

Thanks for that SiCrane.  I'm not sure why exactly I was so focused on writing an entire IsNumber function from scratch.  I actually just got rid of it and will just allow std::stringstream do it's job.

 

Now the ToNumber function works pretty well by just letting std::stringstream do it's job.  I also check what in_avail returned to see if anything is left in the buffer.  It works pretty well.  Only thing is what you brought up.  If I enter a string that is too large to fit in a 32-bit int then I will get a wrong conversion. though the function will think everything was converted just fine.  I at first thought I would use the std::numeric_limits min and max and check if the converted value was less than or greater than those but since the number would just be filled with garbage it would fall within the range (and my testing did confirm this it seems).  So now I am a bit unsure on how to check that the number is within the range allowed for that type.  Any tips for that or something I should look up real fast?

 

Or am I entering something that I really don't want to deal with and should just deal with what I have?  I mean since I really am just doing this for a really quick way to work with and check the input used in the small university projects I get. We aren't really graded for console input/output.  Really on that we understood the basics of Linked List, Binary Search Tree, or something like that.  Limits are also set to allow us to assume as of right now that the user did use the right input for the most part.  So while it would be ok to use what I have really I'd rather have it more complete.

 

NOTE: I don't actually expect it to get fully complete where it would "rival" the C++11 way or the boost::lexical_cast.  Just work for the basic numerical data.  It would never be seen in any production code or something.  I for sure would use C++11 or boost:lexical_cast for that!

 

Thanks for all the help.  I'm just having fun experimenting with this right now just to see what I could come up with.

0

Share this post


Link to post
Share on other sites
If you want to see if an extraction operation failed on a stream check the stream state. If something like the number being too big happens then the stream fail bit will be set, which you can check with the fail() member function. Alternately, you can convert the stream itself to a boolean context which gives you the opposite of the fail() function. Note that this kind of check is in addition to checking if there's still input in the stream buffer.
2

Share this post


Link to post
Share on other sites

If you want to see if an extraction operation failed on a stream check the stream state. If something like the number being too big happens then the stream fail bit will be set, which you can check with the fail() member function. Alternately, you can convert the stream itself to a boolean context which gives you the opposite of the fail() function. Note that this kind of check is in addition to checking if there's still input in the stream buffer.


Got it!
Thanks for all the help! I need to go through the posts and rate up these helpful posts!
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0