0

below is my program.. this concerns about IP addresses. this program works, but I have to separate the implementations in a class in the header file aside from main function.. the implementation/ the codes in the header file must be separated in one file and be conected to the header file and the header file will be connected to the main function.. how would I do this? pls help me..

implementation.cpp -> IpToCountry.hpp -> main.cpp

//IpToCountry.hpp
#include <stdio.h>

class IpToCountry{
    long long int x, y, input;
    char sentence[300], word[10][15];
    int i;
    public:
    void Input(long long int);
    const char* convert(){    
        FILE *fp;
           fp=fopen("IpToCountry.csv","r");
            if(fp==NULL){
            puts("cannot open");}
            else{
                while ( fgets(sentence, 300, fp) != NULL){
                char *ox=strstr (sentence, "#");
                int size=0;
                if(ox==0){
                              char *ptr = strtok(sentence, ",\""); 
                              while ( ptr != NULL ){
                        strcpy(word[size], ptr);
                                   if ( ++size >= sizeof word / sizeof *word ) break;
                                    ptr = strtok(NULL, ",\"");}
                              for ( i = 0; i < size; i++ )
                              {
                                  x=atoll(word[0]);
                                  y=atoll(word[1]);
                              }//for
                              if(input>=x&&input<=y){
                        return word[4];
                                            fclose(fp);
                                            exit(1);}
                              }
                    }//while
                }//else
        puts("WARNING: Invalid IP Number!!!");
        fclose(fp);
        }
};
void IpToCountry::Input(long long int MainInput){
    input=MainInput;}

:rolleyes:

// main.cpp
#include <stdio.h>
#include <iostream>
#include "IpToCountry.hpp"

using namespace std;

int main()
{
long long int MainInput;
cout<< "Please enter the IP Number here:";
cin>> MainInput;
IpToCountry SubFunc;
SubFunc.Input(MainInput);   
cout<< SubFunc.convert()<<endl;
return 0;   
}
2
Contributors
1
Reply
2
Views
10 Years
Discussion Span
Last Post by iamthwee
0
//IpToCountry.hpp
#include <stdio.h>

class IpToCountry
{
   long long int x, y, input;
   char sentence[300], word[10][15];
   int i;
public:
   void Input ( long long int );
   const char* convert()
   {
      FILE *fp;
      fp = fopen ( "IpToCountry.csv", "r" );
      if ( fp == NULL )
      {
         puts ( "cannot open" );
      }
      else
      {
         while ( fgets ( sentence, 300, fp ) != NULL )
         {
            char *ox = strstr ( sentence, "#" );
            int size = 0;
            if ( ox == 0 )
            {
               char *ptr = strtok ( sentence, ",\"" );
               while ( ptr != NULL )
               {
                  strcpy ( word[size], ptr );
                  if ( ++size >= sizeof word / sizeof *word ) break;
                  ptr = strtok ( NULL, ",\"" );
               }
               for ( i = 0; i < size; i++ )
               {
                  x = atoll ( word[0] );
                  y = atoll ( word[1] );
               }//for
               if ( input >= x && input <= y )
               {
                  return word[4];
                  fclose ( fp );
                  exit ( 1 );
               }
            }
         }//while
      }//else
      puts ( "WARNING: Invalid IP Number!!!" );
      fclose ( fp );
   }
};
void IpToCountry::Input ( long long int MainInput )
{
   input = MainInput;
}


// main.cpp
#include <stdio.h>
#include <iostream>
#include "IpToCountry.hpp"

using namespace std;

int main()
{
   long long int MainInput;
   cout << "Please enter the IP Number here:";
   cin >> MainInput;
   IpToCountry SubFunc;
   SubFunc.Input ( MainInput );
   cout << SubFunc.convert() << endl;
   return 0;
}

Why are you mixing c with c++?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.