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:
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.
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.