Need help with accessing an array that hold pointer to the DATA

while (in >> p[i])
{
i++;
}
this is where im having trouble. i know that p is an array that hold the pointer to the DATA but i dont know the syntax to actually access it.

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
  #include <iostream>
#include <fstream>

using namespace std;

class DATA
{
    private:
    int key;
    string firstName;
    string lastName;
    float GPA;

    public:
    friend ifstream &operator >>(ifstream & ,DATA &);
    friend ostream &operator <<(ostream &,DATA);
    int getkey()
    {
        return key;
    };
};

ifstream &operator >>(ifstream &in,DATA &d)
{
    in >> d.key >> d.firstName >> d.lastName >> d.GPA;
    return in;
}

ostream &operator <<(ostream &out,DATA d)
{
    out << d.key << " " << d.firstName << " " << d.lastName << " " << d.GPA;
    return out;
}


class List
{
private:
    DATA **p;
public:
    List();
    int insertDATA (string ,int);
    void getDATA(int);
};


void List::getDATA(int i)
{
    cout << *p[i] << "\n";
}

int List::insertDATA(string fileName ,int i)
{
    ifstream in;
    in.open(fileName.c_str());

    while (in >> p[i])
    {
        i++;
    }
    return i;
}

List::List(void)
{
    p = new DATA*[10];
}


int main(int argc,char *argv[])
{
    List list;
    int counter = 0;
    char option;
    string fileName;

    if (argc == 3)
    {
        fileName = (argv[1]);

        while (counter < 10)
        {
            counter = list.insertDATA(fileName ,counter);
        }

        for (int i = 0; i < counter; i++)
        {
            list.getDATA(i);
        }

        ofstream out;
        out.open(argv[2]);
    }
    else
    {
        cout << "can't open input file and output file: put input file name then follow by output file name";
    }
}
Last edited on
Why is List::p a pointer-to-pointer-to-Data rather than just a pointer-to-Data? In your code, p is an array of pointers-to-Data.
how to i write my p array as a pointer-to-pointer-to-Data?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
int main () {
   int  var;
   int  *ptr;
   int  **pptr;

   var = 3000;

   // take the address of var
   ptr = &var;

   // take the address of ptr using address of operator &
   pptr = &ptr;

   // take the value using pptr
   cout << "Value of var :" << var << endl;
   cout << "Value available at *ptr :" << *ptr << endl;
   cout << "Value available at **pptr :" << **pptr << endl;

   return 0;
}


This example shows that it doesnt matter if i use the pointer to DATA to access my DATA.
In your original code (line 66) you create an array of pointer to DATA. You never create the DATA objects. On line 57 you try to read the data of the stream into the pointer not the DATA.


This example shows that it doesnt matter if i use the pointer to DATA to access my DATA.
Yes, if you do that right (but you doesn't in your original code). However, it doesn't make sense to add another level of indirection which is error prone.
Topic archived. No new replies allowed.