Thread: How to input 16 base IP adress

  1. #1
    Registered User
    Join Date
    Mar 2019
    Posts
    10

    How to input 16 base IP adress

    Hi,how can input base a 16 ip address and check it if it is correct?
    Any helps and tips are appreciated.

    Note:
    You can't use data structures and define your own functions.
    In Base 16 (Hexademical), you can only accept 0..0 and A..F
    Sample output:
    ENTER IP ADRESS:
    AA.FF.00.00
    It's correct
    or
    ENTER IP ADRESS
    AA.256.00.00
    It's wrong

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,268
    I suggest clarifying the implications of "you can't use data structures and define your own functions" with your instructor: an array is a data structure and the main function would be "your own function". Having said that, my guess is that the restriction has more to do with defining structs in C and functions other than the main function.

    I suggest that you start by hardcoding these test IP addresses, valid and invalid, as strings, then think of how you can validate them by analysing the content of these strings. If you can get this right, the rest is just a matter of implementing the interactive I/O to read a string.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Feb 2019
    Posts
    556
    Maybe using scanf with "%2hhX.%2hhX.%2hhX.%2hhX"? Example:

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main( void )
    {
      unsigned char a[4];
      int converted;
    
      const static char ipstr[] = "AA.0.CC.DD";  // should pass.
      const static char ipstr2[] = "AA.256.0.10";  // should fail.
    
      if ( ( converted = sscanf( ipstr, "%2hhX.%2hhX.%2hhX.%2hhX", a, a+1, a+2, a+3 ) ) != 4) 
      {
        if ( converted == EOF )
          perror( "sscanf" );
        else
          fprintf( stderr, "error: Only %d octects converted.\n", converted );
        return EXIT_FAILURE;
      }
    
      printf("%hhu.%hhu.%hhu.%hhu\n", *a, *(a+1), *(a+2), *(a+3));
    
      if ( ( converted = sscanf( ipstr2, "%2hhX.%2hhX.%2hhX.%2hhX", a, a+1, a+2, a+3 ) ) != 4) 
      {
        if ( converted == EOF )
          perror( "sscanf" );
        else
          fprintf( stderr, "error: Only %d octects converted.\n", converted );
        return EXIT_FAILURE;
      }
    
      printf("%hhu.%hhu.%hhu.%hhu\n", *a, *(a+1), *(a+2), *(a+3));
    
      return EXIT_SUCCESS;
    }
    There is also inet_pton(), but it fails with non standard IPv4 or IPv6 addresses...
    Last edited by flp1969; 03-09-2019 at 03:49 PM.

  4. #4
    Registered User
    Join Date
    Mar 2019
    Posts
    10
    Here is the hard copy paste of the question.
    Hope it makes sense now this is my first year in computer science program.We only learned about the basic math operations,if else statements and loops

    BTW I asked the instructor about the strings and we can't use strings.


    Update:
    Please enter the Base for your IP Address (10/16):16
    Please enter the IP Address:
    OO.11.10.KK
    This is NOT a valid IP Address!
    Please enter the IP Address:
    00.11.10.FF
    Thanks it is a valid IP address!
    Please enter the Subnet Mask:
    FF.FF.FF.CO
    Thanks it is a valid Subnet Mask

    Please Note the following for this step:
    An IP address is 32 bit value split into four 8 bit values (octet) each from 0 to 255.Therefore, you need to make the necessary checks for this format.
    In Base 16 (Hexademical), you can only accept 0..0 and A..F
    When the user enters invalid base (other than base 10 and base 16)
    When the users enters invalid IP address or subnet mask


    RULES:
    You are not allowed to use data structures such as arrays to store values for theconversion operation.
    You are not allowed to define your own functions.

  5. #5
    Registered User
    Join Date
    Mar 2019
    Posts
    10
    I have updated the post with more information about the question and rules.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,268
    Sounds like your instructor wants you to validate character by character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NUMA Virtual Adress Space / Physical Adress Space
    By NUMA Orly in forum Windows Programming
    Replies: 0
    Last Post: 03-14-2011, 03:19 AM
  2. Array size base on user input
    By icoigo in forum C Programming
    Replies: 2
    Last Post: 10-27-2010, 12:28 PM
  3. Replies: 5
    Last Post: 07-25-2008, 04:37 AM
  4. Tell adress of object from adress of member
    By TriKri in forum C++ Programming
    Replies: 5
    Last Post: 10-07-2007, 05:04 AM
  5. Replies: 9
    Last Post: 10-07-2006, 05:37 AM

Tags for this Thread