0

I'm struggling to overcome this error. My function is supposed to read data from a file into an array of structures.

Data file example:

1234    52  70  75
2134    90  76  90
3124    90  95  98

Structure definition (average is calculated later):

struct Student
{
     int id, testOne, testTwo, testThree;
    float average;
};
typedef Student student_array[SIZE];

Function:

    void SetArrays (student_array * s)
{
    student_array *ptr = s;

    fstream fin ("lab5_data.txt");

    if (fin.is_open())
    {
        fin >> ptr->id;
    }
}

I can't figure out how to use pointer arithmetic to iterate through the function, this is the errore I get:

[Error] request for member 'id' in '* ptr', which is of non-class type 'student_array {aka Student [20]}'

SOLUTION:

I was able to work through the problem with the advice given here, as well as reddit. Here is my solution, thanks all.

void SetArrays (Student * ptr)
{
    //TODO: Function Spec:
    fstream fin("lab5_data.txt");

    cout << "Data read in:" << endl;
    if (fin.is_open())
    {
        for (int i = 0 ; i < SIZE ; ptr++, i++)
        {
            fin >> ptr->id;
            fin >> ptr->testOne;
            fin >> ptr->testTwo;
            fin>> ptr->testThree;
  • When stuck, try to read the error message over and over again, and see if you can map the various pieces of it to things in the code. Try to understand the message, piece by piece. They feel alien at first, but over time you will learn to understand and appreciate them. – nmr Nov 26 '18 at 19:27
  • @JohnCactus - from your description, it looks like each line in the data file corresponds to a student record. So your assignment is to read each line and populate it into an array. You should look (here)[https://stackoverflow.com/questions/13035674/how-to-read-line-by-line-or-a-whole-text-file-at-once] to know how to read a line from the file. Probably that might clear few things for you. – yasouser Nov 26 '18 at 20:10
  • Also look at this question: [Read file line by line using ifstream in C++](https://stackoverflow.com/questions/7868936/read-file-line-by-line-using-ifstream-in-c) – yasouser Nov 26 '18 at 20:33

2 Answers2

2

ptr is a pointer to an array.

*ptr dereferences the pointer, so the result is an array.

(*ptr)[i] is an element of that array, which is a Student struct.

(*ptr)[i].id is a member of that struct.

Your code is wrong because it tries to dereference ptr with -> (fine so far, ptr is a pointer so dereferencing is a valid operation), but then tries to immediately get an id member, but it's still looking at the whole array. The compiler message roughly means "you told me to get the id field of an array, which makes no sense because arrays only have indices, not named fields".

If you want to use pointer arithmetic, it's easier to get rid of that typedef and pointer-to-array business:

void SetArrays (Student *ptr)
{
    fstream fin ("lab5_data.txt");

    if (fin.is_open())
    {
        while (fin >> ptr->id)
        {
            ptr++;  // advance to the next array element
        }
    }
}

In this version the function takes a pointer to the first element (and uses ++ to step through the array), not a pointer to the whole array.

melpomene
  • 79,257
  • 6
  • 70
  • 127
  • So is it possible to input the data using pointer arithmetic? This is for an assignment, so maybe I'm misunderstanding the instructions? – John Cactus Nov 26 '18 at 19:30
-2

student_array is an array.

You need to read into a given element of that array

like this

fin >> ptr[0]->id;

you probably want a loop and use the loop index as the array index

pm100
  • 32,399
  • 19
  • 69
  • 124