validating a string contains a number

Started by
11 comments, last by graveyard filla 19 years, 7 months ago
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 == 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.
FTA, my 2D futuristic action MMORPG
Advertisement
std::string s;float f;std::cin >> s;try{	if(!(std::istringstream(s) >> f))		throw std::exception("Invalid input.");}catch(std::exception& e) {	std::cout << "Error: " << e.what() << std::endl;}
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.
My code will also handle cases like: .0f, 1e6, 234sdfsdg
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....
FTA, my 2D futuristic action MMORPG
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}
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 bytestd::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?
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]
Another way is to use regular expressions.
Give a man a fish and you feed him for a day; teach him to use the Net and he won't bother you for weeks.
"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.
FTA, my 2D futuristic action MMORPG

This topic is closed to new replies.

Advertisement