Checking the state of a iostream operation

This is a discussion on Checking the state of a iostream operation within the C++ Programming forums, part of the General Programming Boards category; Hi, I've got this method to get an int from a string. Code: bool UtilityClass::___GetIntFromString( const std::string &valueString, int &result ...

  1. #1
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476

    Checking the state of a iostream operation

    Hi, I've got this method to get an int from a string.

    Code:
    bool UtilityClass::___GetIntFromString( const std::string &valueString, int &result )
    {
    	bool retVal = false;
    
    	if (!valueString.empty())
    	{
    		std::istringstream ss;
    		ss.str(valueString);
    		ss >> result;
    		bool isGood = ss.good();
    		std::ios_base::iostate state = ss.rdstate ( );
    		//if (ss.good())
    		if ( (ss.rdstate() & std::istringstream::failbit) == 0 )
    			retVal = true;
    	}
    
    	return retVal;
    }
    I've been having a rather peculiar problem with its error checking (the ones in bold). I tried inputting an alphanumerical (or even alphabetical) string input but the isGood boolean equals to true and the state value is 0. While when I tried inputting a numerical string value, the isGood boolean equals false and the state is 1. Why does this happen? Am I not doing this correctly? Any idea how to do the errorchecking from this particular code? Thanks in advance.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,794
    That code should return true for any input string that begins with a numeric character (even if the rest of the string is not numeric). It returns false if the first character is not numeric. The conversion using the stringstream stops when it encounters the first non-numeric character. The state of the stream is good as long as it is successfully able to convert something from the input. If you want to make sure your entire string is a valid value and not just a portion of it, you can test the remaining length of the data in the stringstream. If everything was converted to int, then there should be nothing left in the stream and it's length should be 0. If there is something left in the stream, length <> 0, then conversion stopped at some point prior to the end of the input string.
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Well apparently the state bit was set to eofbit because it reached eof when the whole string was parsed correctly. So the good() method is always false. Now I just have to check whether the bit was set to eofbit or not.

    Thanks though.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,407
    I suggest:
    Code:
    namespace Utility
    {
        bool GetIntFromString(const std::string& valueString, int& result)
        {
            std::istringstream ss(valueString);
            return (ss >> result) && ss.eof();
        }
    }
    Note that names that begin with an underscore followed by an uppercase letter and names that contain consecutive underscores are reserved to the implementation for any use.
    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

  5. #5
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Yeah thanks. It is actually better to make it simpler using a singleton utility class like what I did right now. But actually, the Utility singleton also has numerous member variables (Actually more like a universal all purpose superclass than utility. I know it's a bad design, but I'm to lazy to refactor. :P). And the underscores are just my style to determine that it is a private one and first capital letter to determine that it is a method inside a class. The same with member variables (eg. ___CallBackup(), etc.). Underscores and first non capital m is a sign that it is a private variable (eg. ___mMyInt, etc).
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,407
    Quote Originally Posted by g4j31a5
    It is actually better to make it simpler using a singleton utility class like what I did right now.
    Why do you think that it is better? I do not see how it is simpler.

    Quote Originally Posted by g4j31a5
    And the underscores are just my style to determine that it is a private one and first capital letter to determine that it is a method inside a class. The same with member variables (eg. ___CallBackup(), etc.). Underscores and first non capital m is a sign that it is a private variable (eg. ___mMyInt, etc).
    The problem is that your style is in conflict with a rule and so you risk undefined behaviour. A simple solution is to only use a single underscore, and place it at the end rather than the beginning of the name.
    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

  7. #7
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by laserlight View Post
    Why do you think that it is better? I do not see how it is simpler.
    Did I really say that? What I mean was it is actually better using your implementation because it is actually simpler instead of my singleton utility class. However, the utility class is a part of a singleton superclass that also consist of the configuration of the application and an instance of the main application's root. I put it into a singleton so that it can be accessed from any part of the code. And yeah, it can actually be divided into different classes but I feel kind of lazy to do so.

    Dunno how I can wrote it like that but that is not what I actually had in mind. Damn, I need a vacation.

    Quote Originally Posted by laserlight View Post
    The problem is that your style is in conflict with a rule and so you risk undefined behaviour. A simple solution is to only use a single underscore, and place it at the end rather than the beginning of the name.
    My bad. Yeah, I've just noticed it after reading the wikipedia and msdn. However, I am using a triple underscore so it shouldn't cause any conflict.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  8. #8
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,407
    Quote Originally Posted by g4j31a5
    However, I am using a triple underscore so it shouldn't cause any conflict.
    However, you are still using reserved names.
    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

  9. #9
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by laserlight View Post
    However, you are still using reserved names.
    From wikipedia: "Names beginning with double underscore or an underscore and a capital letter are reserved for implementation (compiler, standard library) and should not be used (e.g. __reserved or _Reserved)."
    From MSDN: "Use of two sequential underscore characters ( __ ) at the beginning of an identifier, or a single leading underscore followed by a capital letter, is reserved for C++ implementations in all scopes. You should avoid using one leading underscore followed by a lowercase letter for names with file scope because of possible conflicts with current or future reserved identifiers."
    I'm using triple underscores, so how can I still be using reserved names? Or are you saying that I shouldn't use underscore as a prefix at all?
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  10. #10
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,165
    I'm using triple underscores, so how can I still be using reserved names?
    O_o

    Are you kidding? Does "__________A" not begin with an underscore?

    Soma

  11. #11
    Registered User
    Join Date
    Aug 2010
    Location
    Poland
    Posts
    681
    Quote Originally Posted by g4j31a5 View Post
    I'm using triple underscores, so how can I still be using reserved names? Or are you saying that I shouldn't use underscore as a prefix at all?
    Well, you still use double underscore as a prefix, we can consider the third one as a part of the actual name.

    ___Global can be a reserved name, standing for _Global


  12. #12
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Quote Originally Posted by phantomotap View Post
    O_o

    Are you kidding? Does "__________A" not begin with an underscore?

    Soma
    Quote Originally Posted by kmdv View Post
    Well, you still use double underscore as a prefix, we can consider the third one as a part of the actual name.

    ___Global can be a reserved name, standing for _Global

    Ok, I've searched the web and there are actually people that take the simpler approach and read the rule as is (like me) and those that read it in a more creative way (like you guys). My point of view was if any underscore at the beginning of an identifier is evil, why did they just mentioned it like that instead of just explaining that the evil ones are double underscores, a single underscore and capital letter, and a single underscore with small letter. Even the example on Wikipedia didn't mention anything about whether underscores more than 2 is correct or not. The way I see it, it's still in the grey area. So I think I should just drop this issue because it's a no win situation. Thanks a lot though.
    Last edited by g4j31a5; 10-05-2010 at 11:23 PM.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >My point of view was if any underscore at the beginning of an identifier is evil, why did
    >they just mentioned it like that instead of just explaining that the evil ones are double
    >underscores, a single underscore and capital letter, and a single underscore with small letter.

    Because it's not that simple. The underscore prefix as a whole is often touted as evil because unless you're familiar with the lines, it's easy to blunder over them.

    >The way I see it, it's still in the grey area.
    No, it's not gray at all. The precise wording of the standard is that any identifier which contains a double underscore is reserved by the implementation for any use. I'd like to meet the idiot compiler writer who believes _ _ _ doesn't contain _ _. Further, the double underscore need not be at the beginning of an identifier to cause trouble, hence why the rule says "contains" rather than "begins with".
    My best code is written with the delete key.

  14. #14
    In the Land of Diddly-Doo g4j31a5's Avatar
    Join Date
    Jul 2006
    Posts
    476
    Okay. So underscores are bad. But how come I see some libraries using it? One example that I have in mind is Open Scene Graph. Granted it didn't use double or triple underscores. But a single one (eg. _passes, _effects, etc). And I see lots of people using a single underscore at the start of an identifier to determine that it's a private. Won't that violate the "You should avoid using one leading underscore followed by a lowercase letter for names with file scope because of possible conflicts with current or future reserved identifiers." clause? Is it because a single underscore doesn't contain a double underscore? FYI, this is the one line that I don't particularly get.
    ERROR: Brain not found. Please insert a new brain!

    “Do nothing which is of no use.” - Miyamoto Musashi.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,423
    Consider that many people do not know of this little caveat.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Compiling GNU MP
    By mattnp12 in forum C Programming
    Replies: 3
    Last Post: 06-23-2011, 03:58 PM
  2. Buidl Library with ./configure script
    By Jardon in forum C Programming
    Replies: 6
    Last Post: 07-24-2009, 09:36 AM
  3. Profiler Valgrind
    By afflictedd2 in forum C++ Programming
    Replies: 4
    Last Post: 07-18-2008, 09:38 AM
  4. Replies: 16
    Last Post: 11-23-2007, 12:48 PM
  5. Problems about gcc installation
    By kevin_cat in forum Linux Programming
    Replies: 4
    Last Post: 08-09-2005, 09:05 AM

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