Thread: A good way to convert strings to floats?

  1. #1
    Registered User
    Join Date
    Jul 2007
    Posts
    186

    A good way to convert strings to floats?

    I'm trying to convert a string to a float. I can successfully distinguish between numbers and letters BUT if I type in 0 I'm not sure what to do.
    Code:
    string input;
    float x;
    char *end;
    cin >> input;
    x = strtof(input.c_str(),&end);
    if(x>0 || isZero(input))
         //do things
    Code:
    bool isZero(string input)
    {
    	//???
    }

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Check if the string is "empty", and if "endptr" points to a '\0' character.

    Although I would probably say that stringstream is a better C++ solution - or boost:lexical_cast<> for a more advanced solution.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Registered User
    Join Date
    Jul 2007
    Posts
    186
    Thanks!

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by matsp View Post
    Although I would probably say that stringstream is a better C++ solution - or boost:lexical_cast<> for a more advanced solution.
    stringstream is more C++, but at least strtof() gives you a way of checking error status without doing extremely strange things. Due to the nature of iostreams, it's impossible or very hard to check if somebody has entered something like "1.23xxx", since it will parse the 1.23 and leave the "xxx" in the buffer.

    Maybe I should add it to my sig -- input validation is hard. To get 100% coverage you almost always end up writing some custom parser.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by brewbuck
    stringstream is more C++, but at least strtof() gives you a way of checking error status without doing extremely strange things. Due to the nature of iostreams, it's impossible or very hard to check if somebody has entered something like "1.23xxx", since it will parse the 1.23 and leave the "xxx" in the buffer.
    It is not that hard to check with a stringstream. You could just get() and check that EOF is returned. Of course, boost::lexical_cast makes it even simpler, but then you would have to prepare to catch the exception instead of checking some error status. (Then again, that could be a Good Thing, since you are forced to catch the exception or allow it to propagate.)
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 04-29-2009, 10:13 AM
  2. Converting MSVC7 to MSVC8 strings
    By goldwing in forum C++ Programming
    Replies: 3
    Last Post: 10-02-2008, 04:13 PM
  3. linked list recursive function spaghetti
    By ... in forum C++ Programming
    Replies: 4
    Last Post: 09-02-2003, 02:53 PM
  4. finding strings in strings
    By watshamacalit in forum C Programming
    Replies: 14
    Last Post: 01-11-2003, 01:08 AM
  5. convert numbers to strings
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-08-2002, 03:43 PM