Thread: validating a float

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    validating a float

    Is the an easier way, i get errors here

    Code:
    bool valid::validFloat(const std::string& value) 
    {
    	float f;
    	std::istringstream strin(value); /* error C2440: 'initializing' : cannot convert from 'std::string' to 'int' */
    	strin >> f;
    	if (!strin)
    		return false;
    	else 
    		return true;
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Did you remember to include the appropriate headers? Other than that I suggest that you post the smallest and simplest program that demonstrates the problem since what you posted looks like it should not result in a compile error.
    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

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Forgot #include <sstream> ...

  4. #4
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    This is a definite case of MSVC++'s absurd error recovery, and you're missing <sstream>. (There should be three other errors on that line, one of them stating that istringstream is not a member of std or something to that effect.

    Anyway, you can just do
    return strin;
    at the end.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by CornedBee View Post
    Anyway, you can just do
    return strin;
    at the end.
    you mean..

    Code:
    bool valid::validFloat(const std::string& value) 
    {
    	float f;
    	std::istringstream strin(value);
    	return strin >> f;	
    }
    ?

  6. #6
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Or perhaps
    Code:
    return strin >> f && strin.eof();
    since we not only want a float to be parseable, but it should also be the only thing in the stream.
    Note: this fails if you want trailing whitespace to be valid. You could do this:
    Code:
    return strin >> f >> std::ws && strin.eof()
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    610

    setprecision

    May i hijack this thread for another float related...

    I'm trying to diplay 2 decimals after a point but this fails

    Code:
    	cout << "Contract Balance Owed " << setprecision (2) << setw(8) << bal << "\n";
    Any reasons?

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    You just need additional manipulators. fixed suppresses scientific notation. showpoint makes a point visible even when there is a fractional part of zero.

    For example: cout << showpoint << fixed << setprecision(2) << bal << endl;

  9. #9
    Registered User
    Join Date
    Apr 2008
    Posts
    610
    Quote Originally Posted by whiteflags View Post
    You just need additional manipulators. fixed suppresses scientific notation. showpoint makes a point visible even when there is a fractional part of zero.

    For example: cout << showpoint << fixed << setprecision(2) << bal << endl;
    thanx

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 05-13-2009, 03:25 PM
  2. Replies: 14
    Last Post: 06-28-2006, 01:58 AM
  3. Could somebody please help me with this C program
    By brett73 in forum C Programming
    Replies: 6
    Last Post: 11-25-2004, 02:19 AM
  4. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM
  5. How do you search & sort an array?
    By sketchit in forum C Programming
    Replies: 30
    Last Post: 11-03-2001, 05:26 PM