Sign in to follow this  

validating a string contains a number

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, the question is simple. im working on my 2d map editor, and i ask the user questions like "How wide is this map?". i take their input, and put it into a string. then i use string stream to convert it to a number. the problem is, if they put in a regular character and not a number, the program crash's. how could i validate that im about to put a number into the variable. i know i could do this:
bool Is_Number(string str)
{
   for(int i = 0; i < str.size(); i++)
   {
       bool is_num = false;

       for(int j = 0; j <= 10; j++)
       {
          if(str[i] == To_String(j))
              is_num = true;
          
       }

       if(!is_num)
           return false;
   }

      return true;
}




this *should* work. is there a better solution though? also, does randomly accessing a string[] return a character, or a string? i guess im gunna have to google that one. thanks for any help.

Share this post


Link to post
Share on other sites

bool Is_Number(string str)
{
for(int i = 0; i < str.size(); i++)
if(( str.at(i) => '0' && str.at(i) <= '9') == false)
return false;

return true;
}


OR


bool Is_Number(string str)
{
for(int i = 0; i < str.size(); i++)
if(isdigit(str.at(i)) == false)
return false;
return true;
}


EDIT: I shouldn't code when I'm tired.

Share this post


Link to post
Share on other sites
hey guys,

thanks everyone for your replies. i decided to go with Binomine's method since i dont really work with exceptions and i've never used istringstream before, so i didnt really understand how it works.

Why wouldnt Bino's code work in those cases? i dont see why not....

Share this post


Link to post
Share on other sites
Quote:
Why wouldnt Bino's code work in those cases? i dont see why not....


Ofcourse it would work. What i mean is that my code would handle them in a nicer way. (well I think it's nicer) You don't have to use exceptions a simple if...else would work.


std::string s;
float f;

std::cin >> s;

if((std::istringstream(s) >> f)) {
// success
// do something with f
}
else {
// error
}

Share this post


Link to post
Share on other sites
I guess it depends what you mean by "handle"; Binomine's code rejects them and yours grabs the integer at the front of the stream and ignores the rest as garbage.

That's easily changed though:

std::string s;
float f;
char c; // single byte

std::cin >> s;

std::istringstream parser(s);
if((parser >> f) && !(parser >> c)) {
// able to read a float, and there was no byte available to
// be read beyond the float, therefore success
}
else {
// either no float found, or there was some garbage after it
}



The use of exceptions is definitely not called for here.

And for what it's worth, I'm not really sure why you'd bother being this indirect... why not just try to cin into the float directly, and clear/ignore upon failure?

Share this post


Link to post
Share on other sites
The code was just an example I don't think he is using std::cin for input. Did you even read the original post?
Quote:
and yours grabs the integer at the front of the stream and ignores the rest as garbage.

Yes, I know. I don't see that as an error.

[Edited by - NafdahliX on September 19, 2004 4:22:53 AM]

Share this post


Link to post
Share on other sites
"Another way is to use regular expressions."

what do you mean?

anyway, thanks everybody for your help. i decided to stick with Bi's method. it works nice. i mainly used it because i've never used istringstream before, and well, im lazy and dont feel like looking up how it works [smile]. out of curiousity though, how would it handle negative numbers? what if the string contained -2 ??? would it remove that negative, and make it 2? in which case, i couldnt use that method anyway.

thanks again everyone.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Quote:
Original post by graveyard filla
"Another way is to use regular expressions."

what do you mean?


And re "just using cin", you can similarly "just use" whatever other stream the input is coming from. Unless you've got like some Windows widget like a text field that the user is inputting into, or something :s

Share this post


Link to post
Share on other sites

This topic is 4831 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

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