0

I'm trying to pass istringstream and ostringstream by reference to a void function which would extract some substrings from the string passed in istringstream and put them into the ostringstream. The istringstream seems to be working OK, but I am baffled as to how to access the result which should be in ostringstream. Below is my code. When I trace it, I can see that the string in str does get into the function OK, but I'm baffled as to why the results don't appear in strOut. I admit I am a complete noob in working with this stringstream stuff.

void only_uppercase(std::istringstream & iss, std::ostringstream & oss);

int main() 
{

   string str("Hello FROM the DARK side");
   string strOut = "XXXX"; // A string to store the word on each iteration.
    istringstream istr_strm(str);
    ostringstream ostr_strm(strOut);
    only_uppercase(istr_strm, ostr_strm);
    cout << strOut << endl; //Expected: FROM SIDE
 }
  void only_uppercase(std::istringstream & iss, std::ostringstream & oss) 
  {
      std::string word;
      while (iss >> word) {
       bool should_add = true;
       for (char c : word) {
          if (!std::isupper(c)) {
            should_add = false;
            break;
            }
        }
         if (should_add) {
            oss << word << "\n";
        }
    }
}
RonD
  • 9
  • 1
  • Sorry, expected output is FROM DARK not FROM SIDE. Also forgive the rather horrible typos in the heading. That should be: Passing a reference to ostringstream ..... – RonD Mar 04 '21 at 15:53
  • 1
    Well, I guess I should have waited a littl longer to post this question. I stumbled upon the answer as I kept trying things. Anyway, the answer is to use: cout << ostr_strm.str() << endl; //Expected: FROM DARK in the last statement of main – RonD Mar 04 '21 at 16:29

0 Answers0