A good way to convert strings to floats?

This is a discussion on A good way to convert strings to floats? within the C++ Programming forums, part of the General Programming Boards category; I'm trying to convert a string to a float. I can successfully distinguish between numbers and letters BUT if I ...

  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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    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
    21,602
    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.)
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 12:08 AM
  5. convert numbers to strings
    By Unregistered in forum C++ Programming
    Replies: 8
    Last Post: 07-08-2002, 03:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21