Help with pointers!

Hi, I am writing a program for my class where I pass the pointer to an array of scores for separate individuals and store the pointer in an object...

I'm fairly new to pointers and arrays so I was wondering if I am storing each individual student's scores or if my loop is clearing out each student's scores for every iteration. When I printed the address for each student it came out with the same number but as far as I know the loop should be storing them all in separate locations.

Could someone let me know what I need to change in order to make sure each student's test scores are stored and not just erased? Or is there a better way to do this other than an array of scores?

Thanks!

for (int i = 0; i < NUM_STUDENTS; i++)
{
cout <<"Enter student " << i+1 << "'s name: ";
getline(cin,studentName);
cout << "Enter student " << i+1 << "'s address: ";
getline(cin, studentAddress);
cout << "Enter student " << i+1 << "'s GPA: ";
cin >> studentGPA;

for (int j = 0; j < NUM_SCORES; j++)
{
cout << "Enter student " << i+1 << "'s score for test " << j+1 << ": ";
cin >> testScores[j];
}

pointer = testScores;

studentArray[i].setStudentID(rand() % 900000000 + 100000000);
studentArray[i].setStudentName(studentName);
studentArray[i].setStudentAddress(studentAddress);
studentArray[i].setStudentGPA(studentGPA);
studentArray[i].setTestScores(pointer);
cin.ignore();
}
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
#include <iostream>
#include <string>

struct student
{
    int id ;
    std::string name ;
    std::string address ;
    double gpa ;

    enum { NUM_SCORES = 5 };
    int test_scores[NUM_SCORES] ;
};

int main()
{
    const int MAX_STUDENTS = 100 ;
    student student_array[MAX_STUDENTS] ;

    int num_students = 0 ;
    std::cout << "number of students? " ;
    std::cin >> num_students ;
    if( num_students > MAX_STUDENTS ) num_students = MAX_STUDENTS ;

    // extract and discard the new line remaining in the input buffer
    // see: http://www.cplusplus.com/forum/general/63681/#msg344674
    // http://www.cplusplus.com/reference/istream/istream/ignore/ 
    std::cin.ignore( 1000000, '\n' ) ;

    const int id_base = 100000000 ;

    for( int i = 0 ; i < num_students ; ++i )
    {
        student_array[i].id = id_base + i ;

        // note: assumes that all input is successful (no check for input failure)

        std::cout <<"Enter student " << i+1 << "'s name: ";
        std::getline( std::cin, student_array[i].name );

        std::cout << "Enter student " << i+1 << "'s address: ";
        std::getline( std::cin, student_array[i].address );

        std::cout << "Enter student " << i+1 << "'s GPA: ";
        std::cin >> student_array[i].gpa ;

        for( int j = 0; j < student::NUM_SCORES; ++j )
        {
            std::cout << "Enter student " << i+1 << "'s score for test " << j+1 << ": ";
            std::cin >> student_array[i].test_scores[j];
        }

        // extract and discard the new line remaining in the input buffer
        // (assumes that input is terminated with a new line)
        std::cin.ignore( 1000000, '\n' ) ;
    }
}
I assume testScores is a local variable? In that case, yes, it's because you're overwriting in memory.

Lets say we have testScores an array at location 0x1234. On the first loop your array contains [1,2,3,4]. You go studentArray[0].setTestScores(0x1234), so studentArray[0] contains [1,2,3,4] - that is, the memory at 0x1234.

On the next iteration, you set testScores to be [5,6,7,8]. That is, the memory at 0x1234 now contains [5,6,7,8]. Once again, you go studentArray[1].setTestScores(0x1234), so studentArray[1] contains [5,6,7,8] - that is, the memory at 0x1234.

But wait! What about studentArray[0]? It's just pointing to 0x1234, so now it too has contents [5,6,7,8]! Oops.

Basically, what you need to do is to get each studentArray to have it's own array; that is, its own memory location. You may do it something like this:
1
2
3
4
5
6
7
8
9
10
11
12
class Student {
    // local array; our own bit of memory for the scores.
    int testScores[NUM_SCORES];

public:
    // ...
    void setTestScores(int* scores) {
        // copy the data across to our memory
        for (int i = 0; i < NUM_SCORES; ++i)
            testScores[i] = scores[i];
    }
};


Another option is to use a std::vector for your arrays; it will deal with all this pointer nonsense for you behind the scenes, and as an added bonus allows dynamic array length.
Last edited on
Topic archived. No new replies allowed.