0

I am trying to make a server that will take input from the client in postfix notation and send back a result. I have tried a few different things and have ended up using strtok() and strcmp() to find the inputs, but for some reason my server is reading the + symbol as the * symbol.

/* Calculation runs the calculation of the given values */
void calculation (int sock){

 int n;
 char buffer[BUF_SIZE];
 char * n1;
 char * n2;
 char * sym;
 int answer;

 bzero(buffer, BUF_SIZE);
 n = read(sock,buffer, sizeof(buffer));
 if (n < 0) error("ERROR reading from socket");
 printf("Here is the desired calculation: %s\n",buffer);

 n1 = strtok(buffer, " ");
 printf("%s\n", n1);
 n2 =  strtok(NULL, " ");
 printf("%s\n", n2);
 sym =  strtok(NULL, " ");
 printf("%s\n", sym);
 int num1 = atoi(n1);
 printf("%d\n", num1);
 int num2 = atoi(n2);
 printf("%d\n", num2);
 if(strcmp(sym, "+") == 0){
    answer = num1 + num2;
    printf("1 %d\n", answer);
 }
 if(strcmp(sym, "-") == 0){
    answer = num1 - num2;
    printf("2 %d\n", answer);
 }
 if(strcmp(sym, "*") == 0){
    answer = num1 * num2;
    printf("3 %d\n", answer);
 }
 if(strcmp(sym, "/") == 0){
    answer = num1 / num2;
    printf("4 %d\n", answer);
 }
 if(strcmp(sym, "%") == 0){
    answer = num1 % num2;
    printf("5 %d\n", answer);
 }
 printf("%d\n", answer);

 snprintf(buffer, sizeof(buffer), "The result is: %d", answer);
 n = write(sock, buffer, 18);
 if (n < 0) error("ERROR writing to socket");
}

(Edit) Thank you for the advice on responses, I am checking the sym and it outputs it as the right symbol, but as was mentioned in the comments has an extra newline. I'm not sure what is causing it. Thank you for any help.

My output when I use the server is:

Here is the desired calculation: 3 4 +

3
4
+

3
4
32669
  • 3
    `strcmp( ... ) == 1` is the problem. `strcmp` returns 0 if the strings match. – Retired Ninja Apr 22 '22 at 03:38
  • Have you checked the value of `sym`? – Stephen Newell Apr 22 '22 at 03:50
  • And while strcmp is only _required_ to return some nonzero value for unequal, in practice some implementations return the numerical difference between the first two char's that differ, and in any charcode based on ASCII which practically all today are '+'=0x2B is 1 more than '*'=0x2A. – dave_thompson_085 Apr 22 '22 at 03:51
  • 1
    Please [edit](https://stackoverflow.com/posts/71963196/edit) the post with any updates. Not in comments where it can't be formatted properly. – kaylum Apr 22 '22 at 03:53
  • In particular check if `sym` contains something like a plus-sign followed by a newline -- that's not equal to a plus-sign by itself. – dave_thompson_085 Apr 22 '22 at 03:54
  • 3
    The output clearly shows `sym` has an extra newline character at the end of it. So what you have is `strcmp("+\n", "+")` which won't match. Need to strip the newline from `sym` or compare with a string that has a newline character. – kaylum Apr 22 '22 at 04:00
  • Note: you seem to have a single read. TCP doesn't have packets, so you are not guaranteed to get any particular amount of data, like a single line. So, it looks like you are using so called "faith based framing". – hyde Apr 22 '22 at 04:09
  • "*I'm not sure what is causing it*". Your client is sending it. Probably because it is reading a whole line of input with something like `fgets` and sending that over the socket. – kaylum Apr 22 '22 at 04:22
  • To see that none of your `strcmp()` matches, initialize `answer` with some magic value, which will be printed as result. Your compiler should have warned you, BTW. -- The newline character is included in the transmission, for sure. – the busybee Apr 22 '22 at 06:21

0 Answers0