c++ - stringstream to array -
could tell me, why wrong?
i have
mytype test[2]; stringsstream result; int value; (int i=0; i<2; i++) { result.str(""); (some calculating); result<< value; result>> test[i]; }
when watch test array - first - test[0] - has correct value - every other test[1..x] has value 0 why wrong , not working? in first run in cycle stringstream set correct value array, later there 0?
thanks
try result.clear()
ing stringstream before flushing result.str("")
. sets state of accepting inputs again after outputting buffer.
#include <sstream> using namespace std; int main(){ int test[2]; stringstream result; int value; (int i=0; i<2; i++) { result.clear(); result.str(""); value = i; result<< value; result>> test[i]; } return 0; }
without clearing test[0] == 0
, test[1] == -832551553 /*some random number*/
. clear
expected output of test[0] == 0
, test[1] == 1
.
Comments
Post a Comment