user3579220
user3579220

Reputation: 187

Adding Chars to a Stringstream

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

Answers (1)

Anton Savin
Anton Savin

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

Related Questions