New to C++ - Please Help.

This is a discussion on New to C++ - Please Help. within the C++ Programming forums, part of the General Programming Boards category; I am in my first (and last) C++ class and this week we have to write a program to compare ...

  1. #1
    mzt
    mzt is offline
    Registered User
    Join Date
    Mar 2004
    Posts
    5

    Unhappy New to C++ - Please Help.

    I am in my first (and last) C++ class and this week we have to write a program to compare two IP addresses to determine what the class of the IP address is. I have the code working for classes a, b, & c. The problem I am having is trying to code class d & e. I have the code below. Can someone help me?

    I am going to put this into a function (FindClass) after I get the logic working. (Any help with making this function functional is appreciated!!!)

    Code:
    
    #include <iostream.h>
    #include <string>
    #include <fstream.h>
    
    using namespace std;
    	char *addcl;
    
    //int FindClass(*addcl);
    //{	
    //
    //        if(0 < ip[0] && 127 >= ip[0])
    //        {
    //          *addcl = 'a';
    //      }
    //        else if(128 <= ip[0] && ip[0] <= 191)
    //        {
    //            *addcl = 'b';
    //        }
    //        else if(192 <= ip[0] && 223 >= ip[0])
    //        {
    //            *addcl = 'c';
    //        }
    //        else
    //        {
    //            *addcl = 'n';
    //        }
    
    //	return (*addcl);
    //}
    
    
    
    void main(int ip[], char *addcl)
    {
    	int ip1[3], ip2[3];
    	cout << "Please enter an ip address, using spaces between each octet. : ";
    	cin >> ip1[0] >> ip1[1] >> ip1[2] >> ip1[3];
    
    //	cout << "Please enter an ip address, using spaces between each octet. : ";
    //	cin >> ip2[0] >> ip2[1] >> ip2[2] >> ip2[3];
    	 
    //	if(111 == ip1[0] && 99 <= ip1[1])
      //      {
        //       *addcl = 'd';
    	//
    	//	else (111 == ip1[0] && 99 > ip1[1])
    	//	      *addcl = 'e';
    	//	}
               
    	 if(0 < ip1[0] && 127 >= ip1[0])
            {
              *addcl = 'a';
          }
            else if(128 <= ip1[0] && ip1[0] <= 191)
            {
                *addcl = 'b';
            }
            else if(192 <= ip1[0] && 223 >= ip1[0])
            {
                *addcl = 'c';
            }
               
    	   else
            {
                *addcl = 'n';
    	}
    	cout << "IP Addr #1: " <<ip1[0]<<"." <<ip1[1]<<"."<<ip1[2]<<"."<<ip1[3]<<endl;
    //	cout << "IP Addr #2: " <<ip2[0]<<"." <<ip2[1]<<"."<<ip2[2]<<"."<<ip2[3]<<endl;
    	cout << *addcl<<endl;
    
    //	FindClass(ip1[0]);
    //	FindClass(ip2[0]);
    
    }

    THANKS AGAIN!
    Last edited by Salem; 04-24-2004 at 12:25 AM. Reason: Fix "clever" use of code tags which broke code tags

  2. #2
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,659
    Well, class 'D' ranges from 224-239 and 'E' from 240-254.

    Some other problems with the code:

    >> int ip1[3], ip2[3];
    >> cin >> ip1[0] >> ip1[1] >> ip1[2] >> ip1[3];

    - Those two arrays can only hold 3 elements, max, yet you try to access up to 4.
    - Be consistent in the ordering of comparison, ie:
    >> else if(128 <= ip1[0] && ip1[0] <= 191)
    ...is more readable as:
    >> else if(128 <= ip1[0] && 191 >= ip1[0])
    ...or vice versa.
    - Any ip starting with 127 is reserved as the 'loopback' of the machine you are currently running, and doesn't fall into any class. You might want to account for that.

    Also, remember that ip addresses are really just 4-byte unsigned integers. A function that analyzes one then should expect the entire ip - not just the first byte. You might consider something like:

    Code:
     char FindClass(unsigned address)
    {
     char cls = 0;
     char * ip = (char*)&address;
    
         if(ip[0] /* etc */)
       {
        cls = 'a';
       }
        // ...etc
     return cls;
    }



    ITSA
    Socket Library!

Popular pages Recent additions subscribe to a feed

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