Array Bag Smart Pointer Output Help

I want to be able to print out the part that says "The bag contains 1072 items:" and the information after that but but my code prints one item at a time and continues it until it hits 1072 items in bag.



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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#include <array>
#include <iostream>
#include <string>
#include <memory>
#include <fstream>

using namespace std;

enum { INVALID = -1, DEFAULT_CAPACITY = 10 };	// Small size to test for a full bag

template<typename T>
class BagInterface
{
public:
    virtual int getCurrentSize() const = 0;
    
    virtual bool isEmpty() const = 0;
    
    virtual bool add(const T& newEntry) = 0;
    
    virtual bool remove(const T& anEntry) = 0;
    
    virtual void clear() = 0;
    
    virtual int getFrequencyOf(const T& anEntry) const = 0;
    
    virtual bool contains(const T& anEntry) const = 0;
    
};

template<typename T>
class ArrayBag : public BagInterface<T>
{
private:
    unique_ptr<T[]> items;      
    int itemCount;                         
    int maxItems;                          
public:
    ArrayBag() : itemCount(0), maxItems(DEFAULT_CAPACITY)
    {
        items = unique_ptr<T[]>(new T[DEFAULT_CAPACITY]);
    } 
    ArrayBag(int doubledCapacity) : itemCount(0), maxItems(doubledCapacity)
    {
        items = unique_ptr<T[]>(new T[doubledCapacity]);
    }
    int getCurrentSize() const
    {
        return itemCount;
    }  
    
    bool isEmpty() const
    {
        return itemCount == 0;
    } 
    T& get(int index)
    {
        return items.get()[index];
    }
    bool add(const T& newEntry)
    {
        bool hasRoomToAdd = (itemCount < maxItems);
        if (hasRoomToAdd)
        {
            get(itemCount) = move(newEntry);
            itemCount++;
        }  
        if(itemCount == maxItems)
        {
            int capacity = maxItems;
            unique_ptr<T[]> temp = unique_ptr<T[]>(new T[capacity]);
            for (int i = 0; i < capacity; i++)
            {
                temp.get()[i] = move(items.get()[i]);
            }
            
            itemCount = 0;
            capacity *=2;
            items = unique_ptr<T[]>(new T[capacity]);
            for (int i = 0; i < maxItems; i++) {
                items.get()[i] = move(temp.get()[i]);
                itemCount++;
            }
            maxItems = capacity;
        }
        return hasRoomToAdd;
    }  
    bool remove(const T& anEntry)
    {
        int locatedIndex = getIndexOf(anEntry, 0);
        bool canRemoveItem = !isEmpty() && (locatedIndex > INVALID);
        if (canRemoveItem)
        {
            itemCount--;
            get(locatedIndex) = move(get(itemCount));
        }  
        return canRemoveItem;
    }  // end remove
    void clear()
    {
        itemCount = 0;
    }  // end clear
    bool contains(const T& anEntry) const
    {
        return getIndexOf(anEntry, 0) > INVALID;
    }  // end contains
    int getFrequencyOf(const T& anEntry) const
    {
        return countFrequency(anEntry, 0);
    }  // end getFrequencyOf
    int countFrequency(const T& anEntry, int searchIndex) const
    {
        int frequency = 0;
        if (searchIndex < itemCount)
        {
            if (items.get()[searchIndex] == anEntry)
            {
                frequency = 1 + countFrequency(anEntry, searchIndex + 1);
            }
            else
            {
                frequency = countFrequency(anEntry, searchIndex + 1);
            }  // end if
        }  // end if
        
        return frequency;
    }  // end countFrequency
    int getIndexOf(const T& target, int searchIndex) const
    {
        int result = -1;
        if (searchIndex < itemCount)
        {
            if (items.get()[searchIndex] == target)
            {
                result = searchIndex;
            }
            else
            {
                result = getIndexOf(target, searchIndex + 1);
            }  // end if
        }  // end if
        
        return result;
    }  // end getIndexOf
    T& getIndexValue(int index)
    {
        return items.get()[index];
    }
    friend ostream& operator << (ostream& stream, ArrayBag<T>& bag)
    {
        stream << "The bag contains " << static_cast<int>(bag.getCurrentSize()) << " items:" << endl;
        for (int i = 0; i < bag.getCurrentSize(); i++)
        {
            stream << bag.getIndexValue(i) << "";
        }  // end for
        stream << endl << endl;
        return stream;
    }  // end displayBag

};

class Airports
{
private:
    string airportName;
    string airportLatitude;
    string airportLongitude;
    string cityName;
    string stateName;
public:
    Airports()
    {
        airportName = "";
        airportLatitude = "";
        airportLongitude = "";
        cityName = "";
        stateName = "";
    }
    
    Airports(string an, string al, string along, string cn, string sn)
    {
        airportName = an;
        airportLatitude = al;
        airportLongitude = along;
        cityName = cn;
        stateName = sn;
    }
    
    bool operator == (const Airports &an)
    {
        return (an.airportName == airportName);
    }
    
    friend ostream & operator << (ostream & out, const Airports & obj)
    {
        out << obj.airportName << " " << obj.airportLatitude << " " << obj.airportLongitude << " ";
        out << obj.cityName << " " << obj.stateName << endl;
        return out;
    }
    
};

int main()
{
    ArrayBag<Airports> bag;
    
    ifstream inputFile;
    inputFile.open("Airports.csv");
    if (inputFile.is_open())
    {
        while (inputFile.peek() != EOF)
        {
            string airName;
            string airLat;
            string airLong;
            string cityN;
            string stateN;
            
            getline(inputFile, airName, ',');
            getline(inputFile, airLat, ',');
            getline(inputFile, airLong, ',');
            getline(inputFile, cityN, ',');
            getline(inputFile, stateN, '\n');
            
            Airports air(airName, airLat, airLong, cityN, stateN);
            bag.add(air);
            cout << bag;
            
            
        }
        
        inputFile.close();
        
    }
    else
    {
        cout << "Sorry, file not found." << endl;
    }

    return 0;
}  // end main 



OUTPUT:

The bag contains 1 items:
[ABE] 40.65 75.43 Allentown PA


The bag contains 2 items:
[ABE] 40.65 75.43 Allentown PA
[ABI] 32.42 99.68 Abilene TX


The bag contains 3 items:
[ABE] 40.65 75.43 Allentown PA
[ABI] 32.42 99.68 Abilene TX
[ABQ] 35.05 106.60 Albuquerque NM

......

The bag contains 1072 items:
[ABE] 40.65 75.43 Allentown PA
[ABI] 32.42 99.68 Abilene TX
[ABQ] 35.05 106.60 Albuquerque NM
[ABR] 45.45 98.43 Aberdeen SD
[ABY] 31.53 84.18 Albany GA
[ACK] 41.25 70.07 Nantucket MA
[ACT] 31.62 97.22 Waco-Madison TX
[ACV] 40.98 124.10 Arcata CA
[ACY] 39.45 74.57 Atlantic City NJ
[ADK] 51.88 176.65 Adak AK
[ADM] 34.30 97.02 Ardmore OK
ETC. file too long
Last edited on
CODE STARTS HERE!!!!


Please edit your post to make it readable; select all the code in your post, and then while it is selected click on the little button to the right of the box with the symbols <> on it.
Last edited on
Moschops I have edited my post, can you take a look at it now?
closed account (D80DSL3A)
You don't want the info on each item in the bag? Remove lines 152 - 155.
I don't want it to print 1 item and then 2 items and 3 items and so forth, i want to just print out the 1072 items
closed account (D80DSL3A)
I see. That's line 227 providing all that output. Move it outside the while loop?
thank you so much fun2code!! that really helped!
Topic archived. No new replies allowed.