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; I'm using the istringstream class, and created an object of it, passing to its constructor a std::string. Then I use ...

  1. #1
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827

    Question Question about the istringstream >> operator

    I'm using the istringstream class, and created an object of it, passing to its constructor a std::string. Then I use the >> operator multiple times to insert values into an int. I'm wondering if it can insert into a variable which already stores a value or not, since it worked once with one value (1), but failed with another value (2).
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  2. #2
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,590
    IStringStream insertion operator does exactly what it says. It can insert any POD type into the stream as a string.

    To 'insert' into a variable you would use the extraction operator and extract to a string.

  3. #3
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    902
    Code would help. Post yours. Here's mine:
    Code:
    #include <sstream>
    #include <string>
    #include <iostream>
    #include <vector>
    
    int main()
    {
       std::string source("123 14\t22\n1002 -23");
       std::istringstream iss(source);
       std::vector<int> numbers;
    
       int n;
       while(iss >> n)
          numbers.push_back(n);
       //now they're all in, so let's print them.
       std::cout << "Original string: " << source << std::endl;
       for(auto i = numbers.begin(); i != numbers.end(); ++i)
          std::cout << "number: " << (*i) << std::endl;
    
       return 0;
    }
    Last edited by CodeMonkey; 07-09-2010 at 12:31 AM.
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

  4. #4
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Code:
    if (containsString(filestreamInBuffer_ca, "=")) { //last enumerator line contains the '=' character
        cout<< "The last enumerator of the enum has an assigned value." <<endl;
        currentEnumeratorHasAssignedValue_b = true;
    
        //get numerical value of current enumerator:
        currentEnumeratorValue_s = stripString(strippedStr_s.c_str(), '=', ','); //we now have the last enumerator's assigned value in a string
        istringstream currentEnumeratorValue_iss(currentEnumeratorValue_s);
        currentEnumeratorValue_iss>> currentEnumeratorValue_i;
        cout<< "currentEnumeratorValue_i is: " << currentEnumeratorValue_i <<endl;
        enumeratorValues_v_i.push_back(currentEnumeratorValue_i);
    
        //get name of enumerator:
        firstCharOfEnumeratorName_c = filestreamInBuffer_ca[0]; //get first character of line read from file
    
        //strip the line read from the first char of the enum value name to the '=' character, inclusive:
        strippedStr_s = stripString(filestreamInBuffer_ca, firstCharOfEnumeratorName_c, '=', inclusive); 
        lastCharIndex_ui = strippedStr_s.size() - 1; //get index of last character of stripped string
        strippedStr_s.erase(lastCharIndex_ui, 1); //erase last character of stripped string (the '=' character)
        currentEnumeratorName_s = strippedStr_s; //we now have the last enumerator's name
        cout<< "currentEnumeratorName_s is: " << currentEnumeratorName_s <<endl;
        enumeratorNames_v_s.push_back(currentEnumeratorName_s);
        enumeratorsCounter_ui++; //increment the enumerators counter
    
        if (currentEnumeratorHasAssignedValue_b) {
           enumeratorsMap_m_is[currentEnumeratorValue_i] = currentEnumeratorName_s;
           cout<< "enumeratorsMap_m_is.find(" << currentEnumeratorValue_i << ")->second is: " 
                  << enumeratorsMap_m_is.find(currentEnumeratorValue_i)->second <<endl;
           currentEnumeratorHasAssignedValue_b = false; //reset it to false
       }
    
       else {
           if ((enumeratorsCounter_ui != 1) && (currentEnumeratorValue_i != 0)) {
               currentEnumeratorValue_i++;
           }
    
           else {
               if (currentEnumeratorValue_i != 0) { //for some bizarre reason...
                   currentEnumeratorValue_i = 0; //set it to 0 just to be safe
               }
          }
    
          enumeratorsMap_m_is[currentEnumeratorValue_i] = currentEnumeratorName_s;
       }
    }
    The debug output statements show that the first enumerator in an enum read from file has the value 1 (which is correct). The one after that has a value of 2, but it outputs 1. That is why I suspected the istringstream >> operator could not insert a variable which already has a value.

    Here is the enum that its reading:

    Code:
    enum Work {
      hard = 1,
      lazy = 2
     };
    CodeMonkey: Your code wouldn't compile: C++ code - 21 lines - codepad
    Stop monkeying around!
    Last edited by Programmer_P; 07-09-2010 at 07:39 AM. Reason: fixed formatting
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  5. #5
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    o_O
    Try more whitespace between the lines!
    And put those comments above the lines instead of after them. You're causing extreme scrolling for many, I dare say.
    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.

  6. #6
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    o_O
    Try more whitespace between the lines!
    And put those comments above the lines instead of after them. You're causing extreme scrolling for many, I dare say.
    I edited my post and fixed the formatting.
    Should be a lot easier to read now.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    You still have little whitespace between the lines.
    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.

  8. #8
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Fixed.
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  9. #9
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Much better now. But damn, you use long variable names.
    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. #10
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elysia View Post
    Much better now. But damn, you use long variable names.
    I know...
    Its because I prefer to use descriptive names. Notice that each variable name has a _ character followed by some character after it. _s means its a string. _i means its an int. _m_is means a map<int, string>. _v_s means a vector<string>, etc...
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Sigh. Hungarian notation...
    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. #12
    Registered User
    Join Date
    Oct 2006
    Posts
    2,293
    You didn't post any code, so you shouldn't expect any code posted to be completely valid. Most users on here are not going to help you if you do not post code to be evaluated. Without code, we have no idea where your problem actually lies.

    That being said, in the code provided by CodeMonkey, he used the 'auto' keyword, which is a addition to the language in the new C++ standard (actually, it's a re-purposing of an existing keyword). it is used to tell the compiler to infer the type of a variable based on what is being assigned to it, similar to C#'s 'var' keyword. if you have any proficiency at all in C++ you should be able to figure out what should be used in place of 'auto', if you don't have a C++0x-compliant compiler.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,433
    Speaking of comments, you should avoid painfully obvious comments such as:
    Code:
    enumeratorsCounter_ui++; //increment the enumerators counter
    Quote Originally Posted by Programmer_P
    Its because I prefer to use descriptive names. Notice that each variable name has a _ character followed by some character after it. _s means its a string. _i means its an int. _m_is means a map<int, string>. _v_s means a vector<string>, etc...
    Descriptive names are fine. As for Hungarian notation, read Stroustrup's answer to the FAQ: How do you name variables? Do you recommend "Hungarian"?
    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

  14. #14
    Programming Ninja In-T...
    Join Date
    May 2009
    Posts
    827
    Quote Originally Posted by Elkvis View Post
    You didn't post any code, so you shouldn't expect any code posted to be completely valid. Most users on here are not going to help you if you do not post code to be evaluated. Without code, we have no idea where your problem actually lies.
    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.
    That being said, in the code provided by CodeMonkey, he used the 'auto' keyword, which is a addition to the language in the new C++ standard (actually, it's a re-purposing of an existing keyword). it is used to tell the compiler to infer the type of a variable based on what is being assigned to it, similar to C#'s 'var' keyword.
    Yeah, I guessed that, though this is the first time I've seen that keyword used. I Googled it after first reading the code, found this link, which didn't really say that, and in their example, they followed the "auto" keyword by the type of the variable. So I thought maybe it was for something else. Anyway, I did a little tinkering, trying to get CodeMonkey's code to compile, but wasn't able to, and 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?
    I'm an alien from another world. Planet Earth is only my vacation home, and I'm not liking it.

  15. #15
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,437
    Quote Originally Posted by Programmer_P View Post
    ...Anyway, I did a little tinkering, trying to get CodeMonkey's code to compile, but wasn't able to, and 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?
    No, you don't. Use a suitable compiler. Visual Studio 2010 and GCC both supports C++0x.
    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. 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