Question about the istringstream >> operator

This is a discussion on Question about the istringstream >> operator within the C++ Programming forums, part of the General Programming Boards category; Originally Posted by Programmer_P in their example, they followed the "auto" keyword by the type of the variable. So I ...

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,414
    Quote Originally Posted by Programmer_P
    in their example, they followed the "auto" keyword by the type of the variable. So I thought maybe it was for something else. (...) I guess that you would have to specify the type of the vector iterator returned by the vector::begin() function after the "auto" keyword. But, if that's true, what's the point of "auto" to begin with?
    Yes, that is the current usage of auto. As you have found out, it is rather pointless and very, very rarely used, hence the decision was made to sacrifice this in favour of the new semantics of auto in the next version of C++.
    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

  2. #17
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by laserlight View Post
    Speaking of comments, you should avoid painfully obvious comments such as:
    Code:
    enumeratorsCounter_ui++; //increment the enumerators counter
    Right. I don't know why I did that to begin with. I guess because I got in the habit of commenting stuff.
    Descriptive names are fine. As for Hungarian notation, read Stroustrup's answer to the FAQ: How do you name variables? Do you recommend "Hungarian"?
    That's an interesting link (bookmarked it for future reference).
    Thanks.

    Anyway, its all a matter of personal preference. But Stroustrup did make a valid point about possibly running into a problem later on where you change the type of a variable, so the name is no longer accurate when using Hungarian notation. I think I probably will just stick with descriptive names and leave off the hungarian notation in the future. Its just something I recently started doing, thinking it might make things a little more clearer, but I guess not.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  3. #18
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    The string streams work a lot like and are on some level based on the types of streams cin and cout are. This means that you will have all the same issues dealing with string streams -- operations can and will fail on a string stream. So that is probably the crux of the problem.

    While string streams are handy for converting numbers, you can also do basic parsing with string extraction (which works as it does in cin), and other functions, such as get() and ignore(). I recommend this approach to you. Poke your nearest reference.

    Assuming that you will ignore this suggestion, you can also clear() the stream and replace your string when you are ready to convert more strings to numbers. This way you will not confuse the state of the stream versus the source's value.

    Code:
    // mess with your string ...
    
    iss.str(source);
    if (iss >> literalConstant)
    {
       // good
    }
    else
    {
       // bad
    }
    iss.clear();
    
    // mess with source again
    
    iss.str(source);
    if (iss >> literalConstant)
    {
       // and so forth ...
    }

  4. #19
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Hmm...I decided I don't need to clear the istringstream (since it will be a different object of istringstream for each string enumerator value), but clearing the int I'm inserting into after I'm done using it for the current enumerator might work (that way the variable will be recreated in memory with the same identifier for each int). Is there anyway to explicitly destroy a normal stack variable, i.e. something akin to destroying an object on the heap with "delete"?
    Last edited by Programmer_P; 07-09-2010 at 08:13 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #20
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    I can guarantee that string streams do not mishandle PODs like int. In fact if operation fails then the int would stay the same. This is about you not checking for correctness, most likely.

    Is there anyway to explicitly destroy a normal stack variable
    I showed you. The idiom for this is to provide a function that destroys the contained object or resource, e.g. stream.clear(), file.close(), ptr.reset().

    A stack variable's lifetime is also defined by its scope, so you might surround an object declaration and its statements in curly braces, also.

    There is no other option.
    Last edited by whiteflags; 07-09-2010 at 08:25 PM.

  6. #21
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Alright, I answered my own question. There is no problem with the istringstream >> operator inserting into a variable which already has a value, which this code proves:

    C++ code - 21 lines - codepad

    So it seems I need to search for my problem elsewhere...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #22
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,627
    Well maybe, but try following my example first. I mean, I haven't reached the same conclusion you have. Your extraction may just be failing and the literal constant values left alone. Always validate stream operations.

  8. #23
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by whiteflags View Post
    This is about you not checking for correctness, most likely.
    You were correct. I was not checking for correctness, and I'm still not checking for correctness. I'm going to assume the user passes a file with a valid enum. I'm not going to provide support for invalid enums, because it would be too much of a pain trying to handle every single wrong use case (I do warn the user though before the conversion process begins that I didn't provide support for invalid enums).

    Anyway, I fixed that problem (thanks to that comment, which made me think what the problem might be). It turns out the string I was stripping was the wrong one, and so the string being passed to the istringstream object was empty, which is why the value of the int didn't change. Once I changed the code to strip the right string instead, it does as expected (well, not quite...but at least now I'm on to a different problem).

    Thanks for the help everyone.
    Last edited by Programmer_P; 07-09-2010 at 08:49 PM.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #24
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    It isn't difficult to support invalid cases. If your extraction fails, or if you don't get what you expect, then simply fail. Throw an error or whatever.
    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.

  10. #25
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    I did that for brevity. I'm sorry for the confusion it caused. Just declare it with the proper iterator type.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  11. #26
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,428
    I don't think there's any need to apologize. Everyone should learn C++0x sooner or later.
    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.

  12. #27
    Registered User
    Join Date
    Oct 2006
    Posts
    2,292
    Quote Originally Posted by Programmer_P View Post
    Umm...actually, I did post code. Read Post #4.
    Admittedly, I did not post all of it though, because the code's pretty long, and I wanted people to focus on the part that uses the istringstream >> operator to insert values into an int. But, I can post more of it if you want.
    sorry about that... I got distracted while writing my reply, so in the time it took me to write my post, a bunch of other people (including you) posted to the forum, making nearly all of my points quite moot.

  13. #28
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elkvis View Post
    sorry about that... I got distracted while writing my reply, so in the time it took me to write my post, a bunch of other people (including you) posted to the forum, making nearly all of my points quite moot.
    No problem. I figured it was something like that.

    Anyway, I should start getting better at posting all relevant code when making a help request. Something I need to work on in my character. So your post was still helpful.

    Thanks.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

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

Similar Threads

  1. Question about operator overloading
    By h3ro in forum C++ Programming
    Replies: 3
    Last Post: 04-15-2008, 01:02 PM
  2. overloaded >> operator problem
    By quizkiwi in forum C++ Programming
    Replies: 7
    Last Post: 07-19-2005, 03:27 PM
  3. using fstream operator >>
    By Jaken Veina in forum C++ Programming
    Replies: 8
    Last Post: 07-11-2005, 03:57 PM
  4. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM
  5. C number operator question
    By unanimous in forum C Programming
    Replies: 6
    Last Post: 10-26-2001, 09:36 PM

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