validating a float

This is a discussion on validating a float within the C++ Programming forums, part of the General Programming Boards category; Is the an easier way, i get errors here Code: bool valid::validFloat(const std::string& value) { float f; std::istringstream strin(value); /* ...

  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
    21,450
    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.
    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

  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,893
    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,893
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,638
    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, 01: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, 04:26 PM

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