Current开发者_如何学Pythonly I'm working on an assignment and using C++ for the first time. I'm trying to append certain "message types" to the beginning of strings so when sent to the server/client it will deal with the strings depending on the message type. I was wondering if I would be able to put any two-digit integer into an element of the message buffer.... see below.
I've left a section of the code below:
char messageBuffer[32];
messageBuffer[0] = '10'; << I get an overflow here
messageBuffer[1] = '0';
for (int i = 2; i < (userName.size() + 2); i++)
{
messageBuffer[i] = userName[(i - 2)];
}
Thanks =)
'10' is not a valid value, thus the overflow
either write 10 as in messageBuffer[0]=10 - if ten is the value you want to put it or do as Lars wrote.
The message buffer is an array of char. Index 0 contains one char, so you cannot put 2 chars into one char. That would violate the rule that one bit contains one binary digit :-)
The correct solution is to do this:
messageBuffer[0]='0';
messageBuffer[1]='1';
or:
messageBuffer[1]='0';
messageBuffer[0]='1';
or
messageBuffer[0]=10;
精彩评论