I have written some code to show a problem I am having within another program to convert some input strings .
#include <iostream>
#include <sstream>
#include <bitset>
using namespace std;
int main()
{
string tempStr;
unsigned long tempVal;
istringstream tempIss;
bitset<32> tempBitset;
// Input the first word in hex and convert to a bitset
cout << "enter first word in hex : ";
cin >> tempStr;
tempIss.str(tempStr);
cout << "word2 tempIss : " << tempIss.str() << endl;
tempIss >> hex >> tempVal;
cout << "word2 tempVal : " << (int)tempVal << endl;
tempBitset = tempVal;
cout << "word1 tempBitset: " << tempBitset.to_string() << endl;
// Input the second word in hex and convert to a bitset
cout << "enter second word in hex : ";
cin >> tempStr;
tempIss.str(tempStr);
cout << "word2 tempIss : " << tempIss.str() << endl;
tempIss >> hex >> tempVal;
cout << "word2 tempVal : " << (int)tempVal << endl;
tempBitset = tempVal;
cout << "word2 tempBitset: " << tempBitset.to_string() << endl;
return 0;
}
My problem is that whilst the value of tempIss appears to change with the tempIss.str(tempStr);
function, the following tempIss >> hex >> tempVal;
appears to use the old value of tempIss!?
Thanks.
tempIss.str(tempStr);
This merely sets the content of the intenal buffer. It does not, however, reset the error flags that could be set on the stream from previous operations.
Saying tempIss.clear();
before you extract the second time should do the job.