Jump to content

  • Log In with Google      Sign In   
  • Create Account


#ActualChad Smith

Posted 16 March 2013 - 01:40 AM

 

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).


#1Chad Smith

Posted 15 March 2013 - 11:11 PM

 

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.


PARTNERS