Structure Confusion

I'm starting structures, and this program I'm doing is making me confused on how to pass information or even store information on to the object of a structure.
Here is the structure:

1
2
3
4
5
6
7
struct student_info 
{
       string first_name;
       string last_name;
       string year_of_graduation;
};
student_info student_information[5];


And what I'm trying to do is store information into the object array.
I do it with this function:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
string get_student_information(student_info student_information[])
{
       string user_input;
       cout << "Enter in the name, and the year of graduation for each student." << endl;
       for (int x = 0; x < 5; x++)
       {
           cout << "First name: ";
           cin >> user_input;
           student_information[x].first_name = user_input;
           cout << "Last name: ";
           cin >> user_input;
           student_information[x].last_name = user_input;
           cout << "Year of Graduation: ";
           cin >> user_input;
           student_information[x].year_of_graduation = user_input;
       }
       system("CLS");
}


And when I try to display it on another function, the program cannot go on and needs to close. Am I doing the storing right? Cause I looked at a lot of places and can't find the answer I'm looking for. It would help a lot, thanks.

1
2
3
4
5
6
7
8
9
10
11
12
void show_information(student_info student_information[])
{
     int y = 1;
     cout << "=============================================================" << endl;
     cout << "Here is the user information for the students you entered in." << endl;
     cout << endl;
     for (int x = 0; x < 5; x++)
     {
         cout << "Student #" << y << " " << student_information[x].first_name << "   ";
         cout << student_information[x].last_name << "   " << student_information[x].year_of_graduation << endl;
     }
}

that's basically the display function that I'm having trouble with, when it's called, the program closes due to an error, the program compiles, but can't get through this part.
Last edited on
What's the display code?
Also, shouldn't that function return something since its return type is string?
Your function stores the information correctly. Also it does not return any value. If it is what you suppose your function to do, then return value type must be void instead of string.

And also you'd better pass an array to a function by reference instead of passing by value.

Hope this helps :-)

It works! Thank you so much, never thought of that...yet it was so simple ...
Thanks for the help.
Topic archived. No new replies allowed.