how can i walk through a array of pointer?

*p[i] = (ifstream &operator >>(ifstream &in ,DATA & d);
is *p[0] the way i can access to the First DATA in the pointer array?

Infile content

1 john smith 2.0
2 king kong 3.0
3 kevin sim 2.5

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

using namespace std;

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

    public:
    void setAll (int, string, string, float);
    friend ifstream &operator >>(ifstream & ,DATA &);
    friend ostream &operator <<(ostream &,DATA);
};

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

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

void DATA::setAll (int i, string a, string b, float c)
{
    key = i;
    firstName = a;
    lastName = b;
    GPA = c;
}

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

void List::getDATA()
{
    cout << p[0];
}

void List::insertDATA(int i)
{
    ifstream &operator >>(ifstream &in ,DATA & d);
    //*p[i] = (ifstream &operator >>(ifstream &in ,DATA & d);???????????
}

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


int main(int argc,char *argv[])
{
    List list;
    char option;

    for(int i = 0; i < argc; i++)
    {
        cout << argv[i] << endl;
    }

    if (argc == 3)
    {
        ifstream in;
        in.open(argv[1]);

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

        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 should there be a dynamically allocated fixed-size array of pointers to objects?
Why not a simple fixed-sized array of the actual objects?

Something like this, perhaps:
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
#include <iostream>
#include <fstream>
#include <string> // *** required
#include <iomanip>

class Data
{
    public:
        Data() = default ;
        Data( int key, std::string firstName, std::string lastName, double gpa )
            : key(key), firstName(firstName), lastName(lastName), gpa(gpa) {}

        int id() const { return key ; }
        std::string name() const { return firstName + ' ' + lastName ; }
        double grade() const { return gpa ; }

    private:
        int key = 0 ;
        std::string firstName;
        std::string lastName;
        double gpa = 0 ;

};

std::ostream& operator << (  std::ostream& out, const Data& d )
{ return out << d.id() << ' ' << d.name() << ' ' << d.grade() ; }

//ifstream &operator >>(ifstream &in,Data &d)
std::istream& operator >> ( std::istream& in, Data& d )
{
    int key ;
    std::string fname ;
    std::string lname ;
    double gpa ;

    if( in >> key >> fname >> lname >> gpa ) d = { key, fname, lname, gpa } ;
    else d = {} ;

    return in;
}

class List
{
    private:
        static constexpr std::size_t MAX_SZ = 10 ;
        Data p[MAX_SZ] ;
        std::size_t actual_sz = 0 ;

    public:

        void insertdata( Data d )
        {
            if( actual_sz < MAX_SZ )
            {
                p[actual_sz] = d ;
                ++actual_sz ;
            }
        }

        friend std::ostream& operator<< ( std::ostream& stm, const List& lst )
        {
            stm << "[\n" ;
            for( std::size_t i = 0 ; i < lst.actual_sz ; ++i ) stm << "  "  << lst.p[i] << '\n' ;
            return stm << "]\n" ;
        }
};

int main( int argc, char* argv[] )
{
    if( argc > 1 )
    {
        if( std::ifstream in{ argv[1] } ) // if file was opened for input
        {
            List lst ;
            Data d ;
            while( in >> d ) lst.insertdata(d) ;

            if( std::cout << std::fixed << std::setprecision(2) << lst << '\n' ) return 0 ; // success
        }

        else std::cerr << "could not open input file '" << argv[1] << "'\n" ;
    }

    else std::cerr << "invalid command line\n" ; // print usage

    return 1 ; // failure
}

http://coliru.stacked-crooked.com/a/48a688212e85641d
I just have a teacher that like to make thing more complicated than it need to be. I cant use a fix array.
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
#include <iostream>
#include <fstream>
#include <string> // *** required
#include <iomanip>

class Data
{
    public:
        Data() = default ;
        Data( int key, std::string firstName, std::string lastName, double gpa )
            : key(key), firstName(firstName), lastName(lastName), gpa(gpa) {}

        int id() const { return key ; }
        std::string name() const { return firstName + ' ' + lastName ; }
        double grade() const { return gpa ; }

    private:
        int key = 0 ;
        std::string firstName;
        std::string lastName;
        double gpa = 0 ;

};

std::ostream& operator << (  std::ostream& out, const Data& d )
{ return out << d.id() << ' ' << d.name() << ' ' << d.grade() ; }

//ifstream &operator >>(ifstream &in,Data &d)
std::istream& operator >> ( std::istream& in, Data& d )
{
    int key ;
    std::string fname ;
    std::string lname ;
    double gpa ;

    if( in >> key >> fname >> lname >> gpa ) d = { key, fname, lname, gpa } ;
    else d = {} ;

    return in;
}

class List
{
    private:
        static constexpr std::size_t MAX_SZ = 10 ;
        Data** p = new Data*[MAX_SZ]{} ;
        std::size_t actual_sz = 0 ;

        // for brevity: non-copyable, non-moveable
        List( const List& ) = delete ;
        List& operator= ( const List& ) = delete ;
        List( List&& ) = delete ;
        List& operator= ( List&& ) = delete ;

    public:

        List() = default ;
        
        ~List() // we need a destructor
        {
            for( std::size_t i = 0 ; i < actual_sz ; ++i ) delete p[i] ;
            delete [] p ;
        }

        void insertdata( Data d )
        {
            if( actual_sz < MAX_SZ )
            {
                p[actual_sz] = new Data(d) ;
                ++actual_sz ;
            }
        }

        friend std::ostream& operator<< ( std::ostream& stm, const List& lst )
        {
            stm << "[\n" ;
            for( std::size_t i = 0 ; i < lst.actual_sz ; ++i )
                if( lst.p[i] ) stm << "  "  << *(lst.p[i]) << '\n' ;
            return stm << "]\n" ;
        }
};

int main( int argc, char* argv[] )
{
    if( argc > 1 )
    {
        if( std::ifstream in{ argv[1] } ) // if file was opened for input
        {
            List lst ;
            Data d ;
            while( in >> d ) lst.insertdata(d) ;

            if( std::cout << std::fixed << std::setprecision(2) << lst << '\n' ) return 0 ; // success
        }

        else std::cerr << "could not open input file '" << argv[1] << "'\n" ;
    }

    else std::cerr << "invalid command line\n" ; // print usage

    return 1 ; // failure
}

http://coliru.stacked-crooked.com/a/bfe299512f02807e
Hi do you know why when i try to do the same with ostream. I got an run time error? If i remove the ostream and just use a getkey() method in DATA class, it will work but if i try using the ostream, it will give me a run time error?


#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)
{
int KEY;
string FIRST, LAST;
float gpa;

if( in >> KEY >> FIRST >> LAST >> gpa )
{
d.key = KEY;
d.firstName = FIRST;
d.lastName = LAST;
d.GPA = 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;
int y;
public:
List();
void insertDATA (DATA);
DATA getDATA(DATA);
};


DATA List::getDATA(DATA d)
{
for (int y = 0; y < 3; y++)
{
d = *p[y];
}
return d;
}

void List::insertDATA(DATA d)
{
int i = 0;
if (i < 10)
{
p[i] = new DATA(d);
i++;
}
}

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


int main(int argc,char *argv[])
{
DATA d;
List list;
char option;

if (argc == 3)
{
ifstream in;
in.open(argv[1]);

while (in >> d)
{
list.insertDATA(d);
}

cout << list.getDATA(d) << "\n";
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";
}
}
Topic archived. No new replies allowed.