i got this program that read IP addresses from a file and produce a list of distinct addresses and a count of how many times each address appeared in the file. The addresses and counts are stored in a linked list. Your linked list should hold objects of type AddressItem. The thing is when i run the code not all the IP address are output and it seems like some of the duplicate IP's didn't get remove.
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <fstream>
#include "ListPointer.h"
#include "AddressItem.h"
usingnamespace std;
int main()
{
List<AddressItem>IPlist; // List Object of AddressItem type
/*INPUT FILE*/
fstream dataIn;
dataIn.open("IPAddress.txt");
//Checks if the file is available
if(!dataIn){dataIn.close(); cout<<"OOPS! THE FILE YOU ARE TRYING TO OPEN WAS NOT FOUND\n";exit(EXIT_FAILURE);}
//Reads IP address from input file
AddressItem IP, ip; // Objects of AdressItem type ('IP' is the main object & 'ip' is used for comparison)
int i=1, j;
IP.Tally(); //Makes the count 1 since each element must appear at least once
while(!dataIn.eof())
{
IP.Read(dataIn);
if(IPlist.isEmpty()){IPlist.insert(i, IP);} //end of if
elseif(!IPlist.isEmpty())
{
bool noDuplicate=false;
for(j=1;j<=IPlist.getLength();j++)
{
IPlist.retrieve(j, ip);
if (IP==ip)
{
ip.Tally();
IPlist.remove(j);
IPlist.insert(j, ip);
}
}
if (j>IPlist.getLength())
{
noDuplicate=true;
IPlist.insert(i, IP);
}
} //end of else if
i++;
} //end of while
dataIn.close();
cout<<" IP ADDRESS MODE\n";
cout<<"———————————————————————\n";
cout<<"\n*IF NOT REMOVED*\n";
IPlist.display();
cout<<"\n*IF REMOVED*\n";
//removes duplicates
for (int i=1;i<=IPlist.getLength();i++)
{
IPlist.retrieve(i, IP);
for (int j=1+i;j<=IPlist.getLength();j++)
{
IPlist.retrieve(j, ip);
if (IP==ip)
{
IPlist.remove(j);
}
}
}
IPlist.display();
cout<<"\n\n";
return (EXIT_SUCCESS);
} // end of main