Reputation: 561
[SOLVED] See my answer below.
I am trying to use stringstream (named ss) to convert some strings to ints and some to double and such, but the values are all coming out wrong. I have a vector of strings named vec, initialized like this:
[0] = ID # in hex
[1] = phone number
[2] = last name
[3] = first name
[4] = sales number
[5] = percent of sales
Now I go through the vector and put the values into int, string, or double:
std::string lname, fname, pnum;
int ID;
double sales, percent;
if(vec.size()!=6) {
//throw exception
}
else {
ss<<std::hex<<vec[0];
ss>>ID;
pnum = vec[1];
lname = vec[2];
fname = vec[3];
ss<<vec[4];
ss>>sales;
ss<<vec[5];
ss>>percent;
}
but the output looks like this for every customer. What are my ints always 1 and my doubles always this weird number?
Customer made: Langler Tyson 1 7667230284 2.12303e-314 2.12201e-314
Upvotes: 0
Views: 1278
Reputation: 28762
You will need to introduce a separator between the numbers to be ablew to extract them out:
ss << vec[0] << " ";
ss >> ID;
ss << vec[4] << " ";
ss >> sales;
ss << vec[5] << " ";
ss >> percent;
Besides this, you need to revert back to the original decimal format after extracting the ID:
ss << hex;
ss << vec[0] << " ";
ss >> ID;
ss << dec;
Also, because you use the same ss
stream to extract the vector elements, the state of the stream is in error after the last vector element is extracted. You need to clear the error state before further insertion/extraction. Using a new stream will solve the problem as well:
ss = stringstream();
ss << hex;
ss << vec[0] << " ";
ss >> ID;
ss << dec;
ss << vec[4] << " ";
ss >> sales;
ss << vec[5] << " ";
ss >> percent;
Upvotes: 2
Reputation: 561
I got it to work. Here is a screenshot of my whole method: https://i.sstatic.net/tBC8h.png I think that since I used ss to load the vector, reusing it to get the values caused problems. Everything worked correctly when I added the 2nd stringstream ss2.
Upvotes: 1