formating an stdin input

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • momotaro
    Contributor
    • Sep 2006
    • 357

    formating an stdin input

    the idea that am trying to impliment is to read an ip adress intred by the user in the format 'xxx.yyy.zzz.mm m' , detect all possible errors then cut it into xxx, yyy, zzz, mmm, each one must be put in a member of a structure.
    am confused i could play with getc, getchar...
    but plz if u have a better idea and may be easier plz tell me
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #2
    Originally posted by momotaro
    the idea that am trying to impliment is to read an ip adress intred by the user in the format 'xxx.yyy.zzz.mm m' , detect all possible errors then cut it into xxx, yyy, zzz, mmm, each one must be put in a member of a structure.
    am confused i could play with getc, getchar...
    but plz if u have a better idea and may be easier plz tell me
    For a bit of thorough checking read your string (a potential IP/4 address) and:

    1) check if the string length == 15
    2) at positions 3, 7 and 11 should be dots.
    3) at all other positions should be digits.
    4) the three numbers should be in the range [0,255]

    Making an int number out of three consecutive character positions containing
    the ASCII representation of digits is easy.

    scanf("%d" ...) is a nono here because it accepts leading spaces which you
    don't want. The function 'isdigit()' in file ctype.h is your friend.

    kind regards,

    Jos

    Comment

    • momotaro
      Contributor
      • Sep 2006
      • 357

      #3
      but the string is not alway 15!
      for example: 34.54.234.1 is a valide IP adress yet it's not a 15 string lenght!

      Comment

      • momotaro
        Contributor
        • Sep 2006
        • 357

        #4
        am thinking about a function that keps reading my string findind the positions of the dot's then returning them what do you think.?

        Comment

        • JosAH
          Recognized Expert MVP
          • Mar 2007
          • 11453

          #5
          Originally posted by momotaro
          but the string is not alway 15!
          for example: 34.54.234.1 is a valide IP adress yet it's not a 15 string lenght!
          Ah, ok, the rules have to be a bit more relaxed then; are you allowed to use
          regular expression? as in \d+\.\d+\.\d+\. \d+
          That'd be the Java way of doing it; the Boost library has a regular expression
          parser and matcher too.

          If you can't use those follow that pattern 'manually':

          1) read one or more digits
          2) read a dot.
          3) read one or more digits
          4) repeat steps 2) and 3) three times.

          If any of the steps fail or if the number read wasn't in the [0,255] interval,
          the pattern wasn't a valid IP/4 address.

          kind regards,

          Jos

          Comment

          • momotaro
            Contributor
            • Sep 2006
            • 357

            #6
            no we can't but the dot idea solve 90% of the problem still left with how to turn the red digits into integers!

            Comment

            • momotaro
              Contributor
              • Sep 2006
              • 357

              #7
              could I use this function:
              [CODE=c]
              int atoi(const char *s);
              [/CODE]

              Comment

              • JosAH
                Recognized Expert MVP
                • Mar 2007
                • 11453

                #8
                Originally posted by momotaro
                could I use this function:
                [CODE=c]
                int atoi(const char *s);
                [/CODE]
                I think you could if you take a few precautions:

                1) the atoi function stops when it read a non-digit; you have to find where that non-digit is;
                2) atoi() doesn't care about the range of the int; you have to take care of that yourself.

                kind regards,

                Jos

                Comment

                • momotaro
                  Contributor
                  • Sep 2006
                  • 357

                  #9
                  the have been modified(*)

                  pre: - array pos[3] containing the dots' position in the IP adress
                  - array IP[15] containing the IP string
                  post:- return 4 arrays xx[3], yy[3], zz[3], tt[3] containing the IP/4 digits

                  problem(remaini ng from 1st version): - unexpected result while checking using printf
                  example: - instead having "xx[3] = 123" am having 123:)
                  - third part of the IP 123 and some bizard char instead of 123
                  questions: - please help(if there is any other comment...)

                  CODE:
                  [CODE=c]
                  for(i = 0; i < 4; i++)
                  {
                  if(i == 0)
                  {
                  for(j = i; j < pos[i]; j++)
                  {
                  if(isdigit(IP[j]))
                  xx[j] = IP[j];
                  }
                  printf("xxx part\n");
                  printf("%s\n", xx);
                  }
                  else if(i == 1)
                  {
                  for(j = pos[i-1]+1; j < pos[i]; j++)
                  {
                  if(isdigit(IP[j]))
                  {
                  yy[y] = IP[j];
                  y++; //****
                  }
                  }
                  printf("yyy part\n");
                  printf("%s\n", yy);
                  }
                  else if(i == 2)
                  {
                  for(j = pos[i-1]; j < pos[i]; j++)
                  {
                  if(isdigit(IP[j]))
                  {
                  zz[z] = IP[j];
                  z++; //****
                  }
                  }
                  printf("zzz part\n");
                  printf("%s\n", zz);
                  }
                  else if(i == 3)
                  {
                  for(j = pos[i-1]+1; j < 15; j++)
                  {
                  if(isdigit(IP[j]))
                  {
                  mm[m] = IP[j];
                  m++; // ******
                  }
                  }
                  printf("mmm part\n");
                  printf("%s\n", mm);
                  }
                  }
                  [/CODE]

                  Comment

                  Working...