tags:

views:

77

answers:

2

Currently 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 =)

+1  A: 

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;

Lars D
"That would violate the rule that one bit contains one binary digit" Danng ... so that's why I can't get my quantum processor to work :-)
Stephen C
+1  A: 

'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.

Anders K.
Ahhh sweet thanks =D This is really helpful, was really stressed about it.
ej
@ej: If this helped you, you should accept the answer.
sbi