Pointers, lists, and constructors.

Struggling with pointers, lists, and constructors.
The program takes an integer as argv[2] and creates an array based list of size argv[2]. I'm confused as to how to use the constructor.

Please see my comments within.

Thanks,
Jon

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
 #include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include "contactStruct.h"

struct fullContact{
    string lastName, firstName, phoneNumber, emailAddress;
};


class abList{
private:
    fullContact * contactArray;
    int count;
    int numberOfElements;
public:
    //constructor
    abList(int size)
    {
        contactArray = new fullContact[size];
        numberOfElements = 0;
    }
    void addContact(fullContact data)
    {
        contactArray[numberOfElements] = data;
        numberOfElements++;
        //sort inserted data
        if (numberOfElements > 1)
        {
            fullContact temp;
            int lastItem = (numberOfElements-1);
            for (int j = lastItem; j>= 0; j--)
            {
                //they are equal
                if (contactArray[j-1].lastName.compare(contactArray[j].lastName) == 0)
                {
                    //do nothing, because everything 
               //behind it should be in alphabetical order
                    continue;
                }
                //j is greater, alphabetically
                if (contactArray[j-1].lastName.compare(contactArray[j].lastName) > 0)
                {
                    //do nothing. it's in alphabetical order so far
                    continue;
                }
                //j is lesser and should be moved
                if (contactArray[j-1].lastName.compare(contactArray[j].lastName) < 0)
                {
                    
                    temp = contactArray[j];
                    contactArray[j] = contactArray[j-1];
                    contactArray[j-1] = temp;
                }
            }
        }
    }
    fullContact getContact(int pos)
    {
        return contactArray[pos];
    }
    //destructor
    ~abList()
    {
        delete contactArray;
    }
};

using namespace std;

int main(int argc, char* argv[])
{

    bool isAB = true;
    if (argv[2] == NULL)
    {
        isAB = false;
    }
    if (isAB)
    {
        //argv[2] is now size
        int size = atoi(argv[2]);
//Below: What i'm trying to do is construct a new ablist of (size).
//Is this the proper syntax?
        abList * newab = new abList(size);
        
        
    }
        
    ifstream contactFile(argv[1]);
    //if the file is found, open it and import the data into a list
    if (contactFile.is_open())
    {
        string currentLine;
        while ( getline (contactFile,currentLine) )
        {
            stringstream lineStream(currentLine);
            fullContact r;
            getline(lineStream, r.lastName , ',');
            getline(lineStream, r.firstName , ',');
            getline(lineStream, r.phoneNumber , ',');
            getline(lineStream, r.emailAddress , ',');
            cout << r.lastName << endl;
            //add to array based list
            if (isAB)
            {
                cout << "Adding contact to ablist!" << endl;
//Below is not working either. I don't quite understand how to add
//a contact to my newly constructed ab. My addContact function
//should work, should my variables be correct.
//At this point I don't have faith that they are correct.
                newab->addContact(r);
            }
Last edited on
what is contactstruct.h? need the file in order to see the compiler errors
Topic archived. No new replies allowed.