Jump to content

use of array to read student data

- - - - -

  • Please log in to reply
15 replies to this topic

#1
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi

I'm getting this error:
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.


// 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[COLOR="orange"][SIZE="3"][i][/SIZE][/COLOR]);

        }


        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;


}


//-----------------------------------------------------------------

[B][COLOR="red"]void diplay (Student dummystud[])[/COLOR][/B]

{

        cout << "roll no.: " << dummystud[].rollno;

        cout << "name: " << dummystud[].name;

        cout << "age: " << dummystud[].age;

        cout << "marks: " << dummystud[].marks;

        cout << "sex: " << dummystud[].sex;

}

//-----------------------------------------------------------------


Edited by jackson6612, 04 June 2011 - 03:47 PM.

I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
You have to indicate which element of the array you want to output.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
My edit in the previous post appears in orange.

I made edit in this section:

for (i=0; i<N; i++)

        {

                cout << "details of student #" << (i+1) << ":\n";

                display(student[COLOR="orange"][i][/COLOR]);

        }


It's still not working. What do I do? Please help me. Thanks.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#4
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
He was referring to this code:
cout << "roll no.: " << dummystud[].rollno;
dummystud[] does not access an element here. Were you meaning to access the first member?

dummystud[0].rollno;
You could also use a pointer for this:

void display(Student* student) {
   cout << student->rollno;
}
For both you will need to pass the address to the structure as you are accepting an address to a structure.
 display(&student[i]);
You also have misspelled the function definition of display().
 void diplay (Student dummystud[])

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#5
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi Alexander

Thank you for all the help. 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

display(student[i]);


I'm passing an array which is of type struct - namely Student. In the function declarator

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.


// 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";

                [COLOR="blue"]display(student[i]);[/COLOR]

        }


        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;


}


//-----------------------------------------------------------------

[COLOR="royalblue"]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;

}[/COLOR]

//-----------------------------------------------------------------


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#6
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts
Well! You can't pass the whole array by value. It must be passed by reference -- so the function should consist a pointer of the type which your are passing to it as a reference.

To get your job done easily -- you can globalize your array of students, so it will be easy to access and manipulate. Try the code below,

// student_data_structure_readfunc.cpp

// read students' data and find number of A-graded students


#include <iostream>

#include <iomanip>

#include <string>


using namespace std;


////////////////////////////////////////////

struct Student {int rollno, age; string sex, name; float marks;};

////////////////////////////////////////////


const int N = 1;

int A_graded;

struct Student student[N];


void readfunc(int);

void display(int);



int main()

{


        int i;


        for ( i=0; i<N; i++)

        {

                cout << "enter student #" << (i+1) << " details below\n\n";

                readfunc(i);

        }


        cout << "entered details are given below\n\n";


        for (i=0; i<N; i++)

        {

                cout << "details of student #" << (i+1) << ":\n";

                display(i);

        }


        cout << "\n\n";


        return 0;


}


//-------------------------------------------------------------

void readfunc(int i)

{


        cout << "enter roll no.: "; cin >> student[i].rollno;

        cout << "enter name: "; cin >> student[i].name;

        cout << "enter age: "; cin >> student[i].age;

        cout << "enter sex: "; cin >> student[i].sex;

        cout << "enter marks: "; cin >> student[i].marks;


        if ( student[i].marks >= 80)

        {

                A_graded++;

        }


}


//-----------------------------------------------------------------

void display (int i)

{

        cout << "roll no.: " << student[i].rollno;

        cout << "name: " << student[i].name;

        cout << "age: " << student[i].age;

        cout << "marks: " << student[i].marks;

        cout << "sex: " << student[i].sex;

}

//-----------------------------------------------------------------

facing a problem? Feel free to ask.
Hope that helped!

#7
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi AKMafia

Thank you for the help. Your code does make sense.

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:

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, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#8
Alexander

Alexander

    It's Science!

  • Moderators
  • 4,118 posts
  • Location:Vancouver, Eh! Cleverness: 200
There is no reason you would want to pass by value in this case, it creates unnecessary overhead.

You can either use display(&student[i]) or use display(student[i]) if you do the following to your function definition (C++ syntax):
void display(Student& dummystud)

Your dummystud[].rollno; however does not make sense, you should remove the square brackets.

Be sure to read the updated FAQ! || Health is achieved through the same 10,000 steps.
If a suggested code/method fails, informing us is less important than telling us why or what errors occurred.

#9
AKMafia001

AKMafia001

    Programmer

  • Members
  • PipPipPipPip
  • 119 posts

jackson6612 said:

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 "&".
Yes! That's correct. Let's mess up the trouble in your code.

First let's have a look at an example,

#include <iostream>

using namespace std;


void printarray (int arg[], int length) {

  for (int n=0; n<length; n++)

    cout << arg[n] << " ";

  cout << "\n";

}


int main ()

{

  int firstarray[] = {5, 10, 15};

  int secondarray[] = {2, 4, 6, 8, 10};

  printarray (firstarray,3);

  printarray (secondarray,5);

  return 0;

}

output:

5 10 15

2 4 6 8 10

As you see in the above code the function printarray() has an array of int as a parameter, in main() -- we have defined to arrays of type int the first with the lenght 3 and the second with length 5.
These arrays are passed to the function as reference -- as quoted above "reference by default" and the size of these arrays. So in the function the parameter of array of type int refers to the array passed to it by reference and prints it.

Now let's reorganize your code in the above form and see the output,

/*

// student_data_structure_readfunc.cpp

// read students' data and find number of A-graded students


#include <iostream>

#include <iomanip>

#include <string>


using namespace std;


////////////////////////////////////////////

struct Student {int rollno, age; string sex, name; float marks;};

////////////////////////////////////////////


const int N = 5;

int A_graded;


void readfunc(struct Student [], int);

void display(struct Student [], int);



int main()

{


        Student student[N];

        int i;


        for ( i=0; i<N; i++)

        {

                cout << "enter student #" << (i+1) << " details below\n\n";

             	readfunc(student, i);

        }


        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";


        return 0;


}


//-------------------------------------------------------------

void readfunc(struct Student dummystud[], int i)

{


        cout << "enter roll no.: "; cin >> dummystud[i].rollno;

        cout << "enter name: "; cin >> dummystud[i].name;

        cout << "enter age: "; cin >> dummystud[i].age;

        cout << "enter sex: "; cin >> dummystud[i].sex;

        cout << "enter marks: "; cin >> dummystud[i].marks;


        if ( dummystud[i].marks >= 80)

        {

                A_graded++;

        }


}


//-----------------------------------------------------------------

void display (struct Student dummystud[], int i)

{

        cout << "roll no.: " << dummystud[i].rollno << endl;

        cout << "name: " << dummystud[i].name << endl;

        cout << "age: " << dummystud[i].age << endl;

        cout << "marks: " << dummystud[i].marks << endl;

        cout << "sex: " << dummystud[i].sex << endl;

}

//-----------------------------------------------------------------

output:

enter student #1 details below


enter roll no.: 4

enter name: Mafia

enter age: 20

enter sex: Male

enter marks: 100

entered details are given below


details of student #1:

roll no.: 4

name: Mafia

age: 20

marks: 100

sex: Male




In the above code, which belongs to jackson6612 -- the function is having 2 parameters the first is a user-definded data type array and the second is int.
In main(), the array is passed by default as reference to the function and the index. So the user-defined parameter array in the function refers to the actual array which is passed to it as a reference.

was it helpful?
Still Confusion? Ask!

#10
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Thanks a lot, Alexander, AKM.

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 ) "
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? I have tried it myself and it worked. Please have a look on CODE 2.

CODE 1

// 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 = 2;

int A_graded;


Student readfunc();

void display(Student dummystud[]);



int main()

{


        Student student[N];

        int i;


        for ( i=0; i<N; i++ )

        {

                cout << "\n\nenter student #" << (i+1) << " details below\n\n";

                student[i] = readfunc();

        }


        cout << "\nentered details are given below\n\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 display(Student dummystud[])

{

        for ( int i=0; i<N; i++)

        {

                cout << "\n\ndetails for student #" << (i+1) << endl;

                cout << "\nroll no.: " << dummystud[i].rollno;

                cout << "\nname: " << dummystud[i].name;

                cout << "\nage: " << dummystud[i].age;

                cout << "\nmarks: " << dummystud[i].marks;

                cout << "\nsex: " << dummystud[i].sex;

        }

}

//-----------------------------------------------------------------


CODE 2

// 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 = 2;

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 << "\n\ndetails 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++;

        }


        cout << "\n\n";


        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;


        cout << "\n\n";

}

//-----------------------------------------------------------------


I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)

#11
Flying Dutchman

Flying Dutchman

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 889 posts
  • Location:::1
You already have a function that displays data for single student in CODE 2.
A conclusion is where you got tired of thinking.
#define class struct    // All is public.

#12
jackson6612

jackson6612

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 304 posts
Hi Dutchman

Yes, you are correct that I already have a code to display individual array elements. I wanted to know if there is a better and perhaps simpler way to do the task.

Well, let's make a comparison between an array and structure. An array is a kind of macro-variable which contains in itself very much closely related variables. e.g. a human could be an array (or, macro-molecule) and all humans could be its elements (or, individual variables). On the other hand a structure is also a macro-variable which combines attributes of a single object etc into one whole. Please correct me. Thank you.
I'm an outright beginner, learning C++. Using Win XP Pro and Code::Blocks. Be nice to me, please.:)




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users