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.