I am making a game where the answer is stored in client_challenges->answer while the client inputs the answer (which is stored in buffer) in the following format:
A: myanswer
If the answer starts from alphabet A, then i need to compare myanswer with the answer pre-stored. Using the code below, I get the correct buffer and ans lengths but if I print out my store array and answer array, the results differ. For example, if I input A: color, my store gives colo instead of color. However, store-2 works in some cases. How can I fix this?
if (buffer[0] == 'A')
{
printf("ans len %ld, buff len %ld\n",strlen(client_challenges->answer,(strlen(buffer)-4));
if(strlen(client_challenges->answer) == (strlen(buffer)-4))
{
char store[100];
for (int i = 1; i<= strlen(client_challenges->answer);i++)
{
store[i-1]=buffer[2+i];
}
store[strlen(store)-2] = '\0';
//store[strlen(client_challenges->answer)+1]='\0';
printf("Buffer: <%s>\n", buffer);
printf("STORE: %s\n",store);
printf("ANSWER: %s\n",client_challenges->answer);
if(strcmp(store,client_challenges->answer)==0)
{
send(file_descriptor, correct, strlen(correct), 0);
}
}
}
Example: Client enters
A: Advancement
ans len 11, buff len 11
But when I print out store, it is Advancemen
while the answer is Advancement
. However, in my previous attempt, answer was soon and I entered "soon". It worked then.
store[strlen(store)-2] = '\0';
printf()
contained sentinel characters<>
. The"<A: Many"
report seems wrong.store
output with sentinels would have helped rather than just"Many?"
. Recommend,prior toif (buffer[0] == 'A')
, code doesbuffer[strcspn(buffer, "\r\n")] = 0;
to get rid of line endings and also adjust your offset accordingly. Good luck.