URGENT!!

If anyone would be willing to walk me through this I would greatly appreciate it. How would I change the private variables in the header files and the code in the cpp files for the primary indexes so they use a dynamic array or vector instead. For the primary index, the initial vector size will be 8.

header
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
#ifndef MY_PRIMARY_INDEX_H
 #define MY_PRIMARY_INDEX_H
 #include
 #include
 #include
 #include

class PrimaryIndex
 {
 private:
     struct Entry
     {
         //The key is the entry title
         std::string key;
         //the value is the position in the binary file that the indexed entry is located
         int value;

        Entry() : value(-1) {}
         Entry(const std::string& k, int v) : key(k), value(v) {}

    } index[25];

    int count;

public:
     PrimaryIndex()
         : count(0)
     {
     }

    void add_index(const std::string&, int);
     void remove_index(const std::string&);
     int get_value(const std::string&) const;

    int get_count() const;

    void load(const std::string&);
     void save(const std::string&) const;
 };
 #endif 



cpp file

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
#include "PrimaryIndex.h"

void PrimaryIndex::add_index(const std::string& key, int value)
 {
     //Check if index already exists, as no duplicates are allowed
     for(int i = 0; i < count; i++)
     {
         if(index[i].key == key)
         {
             std::cerr << "Cannot add index, already exists." << std::endl;
             return; //early return from function, can't do anything
         }
     }

    //Otherwise, add it to the end
     if(count < 25)
     {
         int pos = count;

        index[pos] = PrimaryIndex::Entry(key, value);

        count++;

    }
     else
     {
         std::cerr << "Cannot add index, index full." << std::endl;
     }
 }

void PrimaryIndex::remove_index(const std::string& key)
 {
     //search for matching key
     for(int i = 0; i < count; i++)
     {
         //check if key matches
         if(index[i].key == key)
         {
             //Move index entries after the remove one forward
             //to fill the gap
             for(int j = i; j < count - 1; j++)
             {
                 index[j] = index[j + 1];
             }

            count--;

            return;
         }
     }

    std::cerr << "Cannot remove index, does not exist" << std::endl;
 }

int PrimaryIndex::get_value(const std::string& key) const
 {
     for(int i = 0; i < count; i++)
     {
         if(index[i].key == key)
         {
             return index[i].value;
         }
     }

    //Indicates not found
     return -1;
 }

int PrimaryIndex::get_count() const
 {
     return this->count;
 }

void PrimaryIndex::load(const std::string& filename)
 {

    count = 0;

    std::ifstream inFile(filename.c_str());

    while(inFile.good() && !inFile.eof())
     {
         PrimaryIndex::Entry entry;

        inFile >> entry.key >> entry.value;

        index[count++] = entry;
     }
 }

void PrimaryIndex::save(const std::string& filename) const
 {
     std::ofstream outFile(filename.c_str());

    for(int i = 0; i < count && outFile.good(); i++)
     {
         outFile << index[i].key << ' '
                 << index[i].value << std::endl;
     }
 }
#include <vector>

Change line 21 of the header file to '};'

Add at line 22 std::vector<Entry> index;

Remove the count variable and replace with calls to index.size();

There are other issues you will run into and have to deal with....

Why do you want to use an array/vector? With a key and a value, you should be using a std::map. It takes care of the single entry per key for you.

@doug4 I am just trying to do my homework as instructed. Thanks for your help! :)
I tried to follow your directions is this right?

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
#ifndef MY_PRIMARY_INDEX_H
#define MY_PRIMARY_INDEX_H
#include <string>
#include <cstring>
#include <iostream>
#include <fstream>
#include<vector>

class PrimaryIndex
{
private:
    struct Entry
    {
        //The key is the entry title
        std::string key;
        //the value is the position in the binary file that the indexed entry is located
        int value;

        Entry() : value(-1) {}
        Entry(const std::string& k, int v) : key(k), value(v) {}
};
    } std::vector<Entry>index[25];

    index.size();

public:
    PrimaryIndex()
        : index.size(0)
    {
    }

    void add_index(const std::string&, int);
    void remove_index(const std::string&);
    int get_value(const std::string&) const;

    int get_count() const;

    void load(const std::string&);
    void save(const std::string&) const;
};
#endif 
Topic archived. No new replies allowed.