Reputation: 187
I am trying to add the numbers only from a char array into a stringstream object. The code is:
char[50] buffer = '<15>';
stringstream str;
int page;
str << buffer[1]+buffer[2];
str >> page;
Page should now hold the integer value of 15, but instead it holds the value 102. Any idea what is wrong with my code?
Upvotes: 1
Views: 8199
Reputation: 41301
Change
str << buffer[1]+buffer[2];
to
str << buffer[1] << buffer[2];
The way your code is written, you add characters '1' and '5', which are equal to 49 and 53 respectively, so you get 102 and output it to the stream.
Upvotes: 3