-
istringstream error
What is wrong with my code? The program does not print the second phrase!
Code:
#include <iostream>
#include <sstream>
int main(){
string buffer(¨This_is_number: 56¨);
string anotherBuffer(¨Initial content¨);
istringstream input(buffer);
input >> buffer;
cout << buffer << endl;
int number;
input >> number;
cout << number << endl;
input.str(¨Another_string¨);
input >> anotherBuffer;
cout << anotherBuffer << endl;
return 0;
}
Thanks for any help.
-
Apparently the stream doesn't like the end of the string directly after the number and goes into fail state.
string buffer("This_is_number: 56\n");
makes it work.
BTW, where do those freaky quotes come from?
-
>What is wrong with my code?
You read input into an EOF state, you cannot read from the stream again until you clear the eof flag:
Code:
#include <iostream>
#include <sstream>
using namespace std;
int main(){
string buffer("This_is_number: 56");
string anotherBuffer("Initial content");
istringstream input(buffer);
input >> buffer;
cout << buffer << endl;
int number;
input >> number;
cout << number << endl;
input.clear();
input.str("Another_string");
input >> anotherBuffer;
cout << anotherBuffer << endl;
return 0;
}
-
Thanks everyone!!! I was trying to use ignore, but didn't work.
CornedBee, I'm not sure what quotes are, but if you are talking about the ¨ in my strings, this is what happens when you use netscape with Linux :P.
Again, thanks to both of you.
-
The eof, of course. Stupid me...