0

I'm trying to read a block of data end with "()" from the user and send it to stringstream, doing some operations on it (for ease now let's said I just want to print these data), and accept a new block of data again, finished this by enter EOF, Here is the code

while(cin >> temp)
    {
        if(temp != "()")
        {
            strem << temp + " ";
        }
        else
        {
            while(strem >> str)
                cout << str << " ";
            cout << "\n";
            strem.str("");
        }

    }

Now after this line strem.str(""); strem didn't read any data in the next loop. I try to debug this by adding this line

cout << strem.str() << "\n"; so the whole code is

while(cin >> temp)
    {
        if(temp != "()")
        {
            strem << temp + " ";
            cout << " strem.str() : " cout << strem.str() << "\n";
        }
        else
        {
            while(strem >> str)
                cout << str << " ";
            cout << "\n";
            //strem.str("");
            strem.str(string());
        }

    }

to see what is the content of the strem, to summarize, it's print the first block, but after that print nothing. any idea what's going on!!

zyydoosh
  • 387
  • 2
  • 14

1 Answers1

2

Doing this

while(strem >> str)

It is almost certain that a flag is set on the stream. So you need to clear stream state before trying to write on it again. You can do this with a

strem.clear()
Doch88
  • 728
  • 1
  • 8
  • 22