Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

String input, pointer to int

Name: Anonymous 2006-04-09 1:19

#include "stdio.h"
#include "string.h"

int _tmain(int argc, _TCHAR* argv[])
{
    char str[80];
    int oct[4];
    int i;
    printf("Enter your IP address:");
    scanf ("%s",str);
    int * pch;
    pch = strtok (str,".");
    for(i=0;i<5;i++)
    {
     printf ("%s\n",pch);
     oct[i]= (pch&);
     pch = strtok (NULL,".");
    }
    system("PAUSE");
    return 0;
}

How would I get this to work? I have been playing with C++, have a program working, and I need a way to break up an IP address and store each octet in an array of integers.  Maybe one of you elite programmers can help a n00b. Thanks.

Name: Anonymous 2006-04-09 4:49

#include <stdio.h>
#include <string.h>

int main(int argc, char **argv)
{
    char s[64];
    int b[4];
    char *p;
    int i;

    /* get the IP */
    printf("Enter your IP address: ");
    fgets(s, 64, stdin);

    /* scan it */
    p = strtok(s, ".");
    for (i = 0; i < 4; i++) {
        sscanf(p, "%d", &b[i]);
        p = strtok(NULL, ".");
    }

    /* and print it back to check */
    printf("IP: %d.%d.%d.%d\n", b[0], b[1], b[2], b[3]);
    return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List