Confusing assignment

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
void person :: print() {
        cout << ssn << " " << fname << " " << lname << " " << dob << "\n";
}
        
person ** read () {
        string arrayname;
        cout << "Please enter the multiple of one thousand you would like to sort" << "\n$
        cin >> arrayname;
        int arraysize = atoi(arrayname.c_str()) * 1000;
        person * * A = new person * [arraysize + 1];
        person * p = new person();
        string filename = "something";
        ifstream fin("something"); //tried putting filename in here but it didn't work
        if (fin.fail()) {
                cerr << "No file\n";
                exit(1);
        }
        for (int i = 0; i <= arraysize; i++) {
                A[i] = p;
                A[i]->print();
                if (i < arraysize)
                        fin >> A[i]->ssn >> A[i]->fname >> A[i]->lname >> A[i]->dob;
                else {
                        A[i]->fname="Mike";
                        A[i]->lname="Tyson";
                }
        }
        fin.close();
        return A;
} 


I'm trying to make it so that my program will take names from a text file and store them in an array until the end where it will place the name Mike Tyson. I'm doing this to make it easier to print later on and whatnot. Is there a reason that after I run this all the names become Mike Tyson?
Is this problem just unsolvable?
It is probably this part:
 
A[i] = p;


All your A[i]'s are pointing to the same person p. So if one A[i] has Mike Tyson as its name ( the last person ) then all have the name Mike Tyson.

Also I'm not sure about this line.
 
person * * A = new person * [arraysize + 1];


It will be better and more understandable if you use standard containers like
std::vector< *person > A;
Last edited on
Topic archived. No new replies allowed.