Help fixing this program.

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.

here is source and the txt thanks in advance.

txtfile
128.159.4.20
123.111.222.333
100.1.4.31
34.56.78.90
120.120.120.120
128.159.4.20
123.111.222.333
123.111.222.333
77.66.55.44
100.1.4.31
123.111.222.333
128.159.4.20

AddressItem.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

#include "ListPointer.h"
using namespace std;

class AddressItem {
public:
AddressItem()
{
count = 0;
}
istream & AddressItem::Read(istream & in)
{
in >> address;
return in;
}

friend ostream & operator<<(ostream & out, const AddressItem & a);

void AddressItem::Tally()
{
count++;
}
bool operator==(const AddressItem & addr2)const
{
return address == addr2.getAddress();
}

string AddressItem::getAddress()const
{
return address;
}

int AddressItem::getCount()const
{
return count;
}

private:
string address;
int count;
};
ostream & operator<<(ostream &out, const AddressItem &a) {
out << "Address: " << a.getAddress() << " Count: " << a.getCount();
return out;
}


#endif 


Listpointer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
ifndef LISTPOINTER_H
#define LISTPOINTER_H 

#include <cstddef>     // for NULL
#include <cassert>     //assert
#include <iostream>
#include <string>
using namespace std;

/** @class List
* ADT list - Pointer-based implementation. */
template<class T>
class List
{
public:
	// Constructors and destructor:

	/** Default constructor. */
	List();

	/** Destructor. */
	~List();

	// List operations:
	bool isEmpty() const;
	int getLength() const;
	void insert(int index, const T& newItem);

	void remove(int index);
	void retrieve(int index, T& dataItem) const;
	void display();


private:
	/** A node on the list. */
	struct ListNode
	{
		/** A data item on the list. */
		T item;
		/** Pointer to next node. */
		ListNode *next;
	}; // end ListNode

	   /** Number of items in list. */
	int size;
	/** Pointer to linked list of items. */
	ListNode *head;

	void find(int index, ListNode *&) const;
}; // end List

   // definitions of methods follow:
template<class T>
List<T>::List() {
	head = NULL;
	size = 0;
}
template<class T>
List<T>::~List()
{
	while (!isEmpty())
		remove(1);
}  // end destructor

template<class T>
bool List<T>::isEmpty() const
{
	return size == 0;
}  // end isEmpty

template<class T>
int List<T>::getLength() const
{
	return size;
}  // end getLength

template<class T>
void List<T>::find(int index, ListNode *& p) const
{
	if ((index < 1) || (index > getLength()))
		p = NULL;

	else  // count from the beginning of the list.
	{
		ListNode *cur = head;
		for (int skip = 1; skip < index; skip++)
			cur = cur->next;
		p = cur;
	}  // end if
}  // end find

template<class T>
void List<T>::retrieve(int index, T& dataItem) const
{
	assert((index >= 1) && (index <= getLength()));

	// get pointer to node, then data in nod
	ListNode *cur;
	find(index, cur);
	dataItem = cur->item;

}  // end retrieve

template<class T>
void List<T>::insert(int index, const T& newItem)
{
	int newLength = getLength() + 1;

	assert((index >= 1) && (index <= getLength() + 1));

	ListNode *newPtr = new ListNode;
	size = newLength;
	newPtr->item = newItem;

	// attach new node to list
	if (index == 1)
	{  // insert new node at beginning of list
		newPtr->next = head;
		head = newPtr;
	}
	else
	{
		ListNode *prev;
		find(index - 1, prev);
		// insert new node after node to which prev points
		newPtr->next = prev->next;
		prev->next = newPtr;
	}  // end if


}  // end insert

template<class T>
void List<T>::remove(int index)
{
	ListNode *cur;

	assert((index >= 1) && (index <= getLength()));

	--size;
	if (index == 1)// delete the first node from the list
	{
		cur = head;  // save pointer to node
		head = head->next;
	}

	else
	{
		ListNode *prev;
		find(index - 1, prev);// delete the node after the node to which prev points
		cur = prev->next;  // save pointer to node
		prev->next = cur->next;
	}
	cur->next = NULL;
	delete cur;
	cur = NULL;
}

template<class T>
void List<T>::display()
{
	ListNode *cur;
	if (size != 0)
		for (cur = head; cur != NULL; cur = cur->next)
			cout << cur->item << endl;
}

#endif; 


listdriver.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <string>
#include <fstream>

#include "ListPointer.h"
#include "AddressItem.h"

using namespace 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
        
        else if(!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 


Last edited on
Why do you have to make your code that complex?
Topic archived. No new replies allowed.