TYpe Conversion Problem

This is a discussion on TYpe Conversion Problem within the C Programming forums, part of the General Programming Boards category; I am writing a socket network program where the user can enter ADD 4 5 etc SUB 5 3, etc ...

  1. #1
    Registered User
    Join Date
    Mar 2006
    Posts
    2

    TYpe Conversion Problem

    I am writing a socket network program where the user can enter ADD 4 5 etc SUB 5 3, etc

    The user would enter it and it is stored into a char array and then sent to the server, the server needs to take it and convert/split it to string int int perform the function and return the result. For some reason the conversion to int is not working:

    (for testing purposed I have the server printing out the three splits of the sting)

    here is an example

    Client:

    ADD 7 6

    Server:

    ADD Num1: -107 Num2: 007

    Here is the server code where the conversion should take place, and suggestions, I thought I had all the converions performed correctly:

    Code:
     
     char temp[4];
        int temp1, temp2;
        n = recv(ns, &buf, sizeof(buf), 0); 
        sscanf(buf, "%s %d %d",temp, &temp1, &temp2);
       
        if (temp[0] == 'A' && temp[1] == 'D' && temp [2] == 'D')
          write (1,"ADD",4);
        else if (temp[0] == 'B' && temp[1] == 'Y' && temp[2] == 'E')
          exit(0);
        // Contine else if for SUB, MUL, DIV...
    
        ////////////////////////////
        write (1,temp,4);
        write(1," Num1: ",7);
        sprintf(temp, "%d", temp1);
        write (1,temp,4);
        write(1," Num2: ",7);
        sprintf(temp, "%d", temp2);
        write(1,temp,4);
        ////////////////////////////
    
        send(ns, temp, sizeof(temp), 0);

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    > n = recv(ns, &buf, sizeof(buf), 0);
    I could be wrong but do you need the address operator (&) on buf? Maybe just:
    Code:
        n = recv(ns, buf, sizeof(buf), 0);
    > sprintf(temp, "%d", temp1);
    > write (1,temp,4);
    And when you write out temp after an sprintf(), why not use strlen:
    Code:
        sprintf(temp, "%d", temp1);
        write (1, temp, strlen(temp));
    Include <string.h> for strlen.

  3. #3
    Registered User
    Join Date
    Mar 2006
    Posts
    2
    Thanks for the suggestions, but making those changes do not fix the problem

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,452
    > char temp[4];
    There's no way you're fitting "ADD 7 6" in there.
    Even if you make the buffer large, you still need to take care of message fragmentation yourself.

    Something like
    Code:
    while ( n = recv(ns, buf, sizeof(buf)-1, 0) ) > 0 ) {
      // append a \0 to make it a proper string
      buf[n] = '\0';
      // append the received substring to your result
      // if the result now contains a unique marker, say a \n
      // then you can quit this loop and do your sscanf
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting the matrix question..
    By transgalactic2 in forum C Programming
    Replies: 47
    Last Post: 12-22-2008, 02:17 PM
  2. how do you resolve this error?
    By -EquinoX- in forum C Programming
    Replies: 32
    Last Post: 11-05-2008, 03:35 PM
  3. Script errors - bool unrecognized and struct issues
    By ulillillia in forum Windows Programming
    Replies: 10
    Last Post: 12-18-2006, 03:44 AM
  4. Creation of Menu problem
    By AQWst in forum C Programming
    Replies: 8
    Last Post: 11-24-2004, 08:44 PM
  5. qwerty/azerty keyboard type problem + question about loop.
    By Robin Hood in forum C++ Programming
    Replies: 9
    Last Post: 07-22-2002, 01:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21