use of array to read student data

This is a discussion on use of array to read student data within the C++ Programming forums, part of the General Programming Boards category; Hi I'm getting this error: Code: error: expected primary-expression before ']' token| for every line in the display() function. Please ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    254

    use of array to read student data

    Hi

    I'm getting this error:
    Code:
    error: expected primary-expression before ']' token|
    for every line in the display() function. Please help me to correct. I'm learning arrays. Thank you for your help and time.

    Code:
    // student_data_structure_readfunc.cpp
    // read students' data and find number of A-graded students
    
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    ////////////////////////////////////////////
    struct Student {int rollno, age; string sex, name; float marks;};
    ////////////////////////////////////////////
    
    const int N = 5;
    int A_graded;
    
    Student readfunc();
    void display(Student dummystud[]);
    
    
    int main()
    {
    
            Student student[N];
            int i;
    
            for ( i=0; i<N; i++)
            {
                    cout << "enter student #" << (i+1) << " details below\n\n";
                    student[i] = readfunc();
            }
    
            cout << "entered details are given below\n\n";
    
            for (i=0; i<N; i++)
            {
                    cout << "details of student #" << (i+1) << ":\n";
                    display(student);
            }
    
            cout << "\n\n";
    
            system("pause");
            return 0;
    
    }
    
    //-------------------------------------------------------------
    Student readfunc()
    {
            Student dummystud;
    
            cout << "enter roll no.: "; cin >> dummystud.rollno;
            cout << "enter name: "; cin >> dummystud.name;
            cout << "enter age: "; cin >> dummystud.age;
            cout << "enter sex: "; cin >> dummystud.sex;
            cout << "enter marks: "; cin >> dummystud.marks;
    
            if ( dummystud.marks >= 80)
            {
                    A_graded++;
            }
    
            return dummystud;
    
    }
    
    //-----------------------------------------------------------------
    void diplay (Student dummystud[])
    {
            cout << "roll no.: " << dummystud[].rollno;
            cout << "name: " << dummystud[].name;
            cout << "age: " << dummystud[].age;
            cout << "marks: " << dummystud[].marks;
            cout << "sex: " << dummystud[].sex;
    }
    //-----------------------------------------------------------------
    Last edited by jackson6612; 06-04-2011 at 06:14 PM.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,658
    When you do

    student[i]

    you're really doing

    *(student + i); because you add the size of i objects in bytes to the base address.

    so if you try doing

    student[]

    what is wrong with the picture?

    Just look carefully and you'll see ALL your mistakes.

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi

    I made a change but it still didn't work. What should I do now?
    Code:
    for (i=0; i<N; i++)
            {
                    cout << "details of student #" << (i+1) << ":\n";
                    display(student[i]);
            }
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,246
    You need to be clear whether display() is receiving an array of Student, or a single one. At the moment the caller is telling the compiler one thing (passing a single Student) but the function declaration and implementation is telling the compiler something else (the function accepts an array of Student).

    student[i] in the caller is a single object of type Student. If that's what you intend to pass, then display() needs to accept a single Student by value (remove the [] from the argument list). Alternatively, if you intend that display() receives an array of Student, then you need to change the caller so it passes an array.

    Compilers are pedantic and ignorant. Your code needs to be written in a precise and pedantic manner to avoid confusing the compiler (or the linker, which receives the output of the compiler, and also behaves pedantically and ignorantly). A caller passing a single object to a function expecting an array is imprecise (and a source of confusion for the compiler or linker).


    Note: the line you highlighted in read in your first post has a typo. "display" needs an s.
    Last edited by grumpy; 06-04-2011 at 07:37 PM.
    Right 98% of the time, and don't care about the other 3%.

  5. #5
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    jackson6612, your error message is an indication as to what's wrong with your logic in the code.

    in the prototype for display:
    Code:
    void display(Student dummystud[]);
    you tell display to accept an array of Student type.

    this means there is more than one struct you want to iterate over right? however in your definition you don't have your code do that.

    In essence you need to make sure your display function goes over all the Student structures not just one or the first one.

    Code:
    void diplay (Student dummystud[]){     
      
            for each struct in the array:
               display dummystudNum.data
            
    }

  6. #6
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Hi

    I'm sorry to ask this again but I'm still having difficulty understanding it. I hope you won't mind my asking again.

    Here in the call
    Code:
    display(student[i]);
    I'm passing an array which is of type struct - namely Student. In the function declarator
    Code:
    void display(Student dummystud[])
    I have also made it clear that the function would accept an argument which is an array of type Student (here Student is a structure). So, what's the problem. The function should work as long as I'm passing on arrays of type Student to it.

    Code:
    // student_data_structure_readfunc.cpp
    // read students' data and find number of A-graded students
    
    #include <iostream>
    #include <cstdlib>
    #include <iomanip>
    #include <string>
    
    using namespace std;
    
    ////////////////////////////////////////////
    struct Student {int rollno, age; string sex, name; float marks;};
    ////////////////////////////////////////////
    
    const int N = 5;
    int A_graded;
    
    Student readfunc();
    void display(Student dummystud[]);
    
    
    int main()
    {
    
            Student student[N];
            int i;
    
            for ( i=0; i<N; i++ )
            {
                    cout << "enter student #" << (i+1) << " details below\n\n";
                    student[i] = readfunc();
            }
    
            cout << "entered details are given below\n\n";
    
            for ( i=0; i<N; i++ )
            {
                    cout << "details of student #" << (i+1) << ":\n";
                    display(student[i]);
            }
    
            cout << "\n\n";
    
            system("pause");
            return 0;
    
    }
    
    //-------------------------------------------------------------
    Student readfunc()
    {
            Student dummystud;
    
            cout << "enter roll no.: "; cin >> dummystud.rollno;
            cout << "enter name: "; cin >> dummystud.name;
            cout << "enter age: "; cin >> dummystud.age;
            cout << "enter sex: "; cin >> dummystud.sex;
            cout << "enter marks: "; cin >> dummystud.marks;
    
            if ( dummystud.marks >= 80 )
            {
                    A_graded++;
            }
    
            return dummystud;
    
    }
    
    //-----------------------------------------------------------------
    void display(Student dummystud[])
    {
            cout << "roll no.: " << dummystud[].rollno;
            cout << "name: " << dummystud[].name;
            cout << "age: " << dummystud[].age;
            cout << "marks: " << dummystud[].marks;
            cout << "sex: " << dummystud[].sex;
    }
    //-----------------------------------------------------------------
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  7. #7
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    There are two problems:
    First, your function accepts an array. But you are passing a single element, not an array. You can see this because student is an array of n students, and you say, pass element n of this array to display.
    Second, your display function takes an array, but the cout statements doesn't say which student to print out. The "[]" syntax here is invalid. If you mean to say, print all students' rollno, then that's impossible. The compiler can't understand it. So what you need to do is tell the compiler to print a single student's rollno. And you basically have to repeat that for every student. This will typically mean a loop. Which means you need to pass along the size of the array (eg, number of students). This is an ideal use of a std::vector.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  8. #8
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by Elysia View Post
    There are two problems:
    First, your function accepts an array. But you are passing a single element, not an array. You can see this because student is an array of n students, and you say, pass element n of this array to display.
    Second, your display function takes an array, but the cout statements doesn't say which student to print out. The "[]" syntax here is invalid.
    Thank you, Elysia.

    I'm so sorry. I still don't get it. Let me tell you what I think about it.

    I have read that when you call a function with an array as an argument, the value of the array is passed by reference by default - you don't need to use "&".

    I still can't recognize the problem with my own code.

    Let's say when "( i = 2 )" the function is called:
    Code:
    display(student[2]);
    "student[2]" is the third array of type Student. The function also expects an array of this type - of type Student and of single dimension.

    Where do I go wrong? Please guide me on this. Thanks.
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  9. #9
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    Quote Originally Posted by jackson6612 View Post
    Thank you, Elysia.

    I'm so sorry. I still don't get it. Let me tell you what I think about it.

    I have read that when you call a function with an array as an argument, the value of the array is passed by reference by default - you don't need to use "&".

    I still can't recognize the problem with my own code.

    Let's say when "( i = 2 )" the function is called:
    Code:
    display(student[2]);
    "student[2]" is the third array of type Student. The function also expects an array of this type - of type Student and of single dimension.

    Where do I go wrong? Please guide me on this. Thanks.
    An array is a block of contiguous memory - meaning your most likely going to have more than one element in that array. Secondly, the name of an array is the address of the first element in that array.

    In your case you have N elements. In the following code you give the address of the first element in your array.
    Code:
    cout << "details of student #" << (i+1) << ":\n";
                        display(student);
    Fine.


    In your definition here you tell the compiler to expect an array again.
    Code:
    void display(Student dummystud[])
    Fine.

    However in the ensuing cout statements you don't tell the compiler to actually go over all the elements in that array.
    Code:
            cout << "roll no.: " << dummystud[].rollno;
            cout << "name: " << dummystud[].name;
            cout << "age: " << dummystud[].age;
            cout << "marks: " << dummystud[].marks;
            cout << "sex: " << dummystud[].sex;
    }

    an array is a block of contiguous memory. your compiler knows this and is expecting you to tell it which element to display. So how would you make it display all the elements in your array?

    that is what this error
    Code:
     error: expected primary-expression before ']' token
    is telling you. It's not going to magically go over each element for you. You have to tell it to.

    something needs to be between those brackets
    Last edited by caroundw5h; 06-05-2011 at 06:55 AM.
    Warning: Opinions subject to change without notice

    The C Library Reference Guide
    Understand the fundamentals
    Then have some more fun

  10. #10
    Registered User
    Join Date
    Mar 2011
    Posts
    254
    Quote Originally Posted by caroundw5h View Post

    In your definition here you tell the compiler to expect an array again.
    Code:
    void display(Student dummystud[])
    Fine.

    However in the ensuing cout statements you don't tell the compiler to actually go over all the elements in that array.
    Code:
            cout << "roll no.: " << dummystud[].rollno;
            cout << "name: " << dummystud[].name;
            cout << "age: " << dummystud[].age;
            cout << "marks: " << dummystud[].marks;
            cout << "sex: " << dummystud[].sex;
    }
    Thanks a lot, caroundw5h.

    I think I understand the problem to some degree. I want the function to print only one element of the array student.

    Here when " ( i = 1 ) "
    Code:
    display(student[i]);
    I actually want the display() to display only the 2dn element of the array, not the complete array. But in the prototype and definition of the display() I have used the idea of 'full' array - I haven't made it clear to it that I want it to display only one element of the array at a time. It expects an array in all its entirety.

    One solution to the problem would be to use the for loop inside the display(). But I don't wanna do that! How do I modify the display() so that it can display individual elements of the array? Please let me know. Thank you all of you for all the guidance and help.

    Best wishes
    Jackson
    I'm an outright beginner. Using Win XP Pro and Code::Blocks. Be nice to me, please.

  11. #11
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,537
    You might want to pass the actual student you wish to print to the function.
    If you have an array of students, students, then you might pass an individual student to the function using students[n] where n is the nth student (eg, the student in the array you want to pass).
    Consequently, you need to tell the compiler that the function accepts a student, so it would be

    Code:
    void display(const Student& student)
    const just tells the compiler that we shall not modify this student. We don't want to do that since we're just printing it.
    And the "&" is just for efficiency's sake, so we don't make a needless copy.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User caroundw5h's Avatar
    Join Date
    Oct 2003
    Posts
    751
    I haven't made it clear to it that I want it to display only one element of the array at a time.
    exactly.

    It expects an array in all its entirety.
    not necessarily. All the compiler needs to know is the address of the first element of the array, and again the name of an array is the address of the first element in that array.

    From there it knows where to start and the number of bytes to increment to get to the next element. You just have to tell it what element you want it to display, whether that be one or all.

    One solution to the problem would be to use the for loop inside the display().
    Yes that is a solution.

    But I don't wanna do that! How do I modify the display() so that it can display individual elements of the array?
    You understand the logic of it now and that is what's important. Putting a loop inside the display function would accomplish what you want, any other way you want to go about displaying the info I'm leaving that as an exercise for you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-30-2010, 09:49 AM
  2. Read size of data array when reading .txt data
    By Taquito in forum C Programming
    Replies: 13
    Last Post: 04-29-2007, 01:52 AM
  3. Replies: 1
    Last Post: 09-10-2005, 06:02 AM
  4. Function to read data from file to array
    By anatazi in forum C Programming
    Replies: 3
    Last Post: 07-01-2002, 11:08 PM
  5. how do i read this data from a file instead of this array
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 06-16-2002, 11:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21