Well the biggest problem you are having is that you are trying to use a school struct and passing it to arrays that are expecting integers. My guess is that you are adapting this code from an example you have seen which uses integers. Just remember you must define them as accepting and handling structures.
Another problem you were having is that you defined your struct inside of main. Define them outside of main. That way everyone knows what a school struct is and looks like.
Yet another problem comes from the actual comparing of the students. Since the compiler has no way of knowing how to compare to school structs, you have to tell it to compare the structures members. In this case the student's id. Now if you had defined operators for your struct, then the compiler could determine how to directly compare their worth against one another.
In this case we just tell the compiler to compare each students id and sort based on that.
The last problem comes from how you are loading and accessing your students in the array. You must specify a subscript when using the array to get at each student. You can't simply use "table" because table is a whole array. You have to tell it which student in the array you want. "table[1]" gets the second student of the array and that itself is a struct of type "school".
Here is your code with comments to help you see what is going on...
cpp
#include <iostream>
#include <iomanip>
using std::cout;
using std::cin;
using std::endl;
using std::setw;
// These prototypes need to define the passing of school structs, not integers
void selection_sort(struct school a[], int length);
int minimum_from(struct school a[], int position, int length);
void swap(school& first, school& second);
// Define school structure outside of main
struct school
{
int student_id;
char name[30];
char grade[2];
};
int main()
{
struct school table[5] = {0};
cout << "Enter information for 5 students below" << endl;
// Collect student info, but remember it is in an array, so you have to access each through the subscript.
// Notice the use of [i] to get at each student struct in the array
for (int i=0; i<5; i++)
{
cout << "Student ID " << i+1 << ": ";
cin >> table[i].student_id;
cin.get();
cout << "Name " << i+1 << ": ";
cin.getline((table[i].name), 30);
cout << "Grade " << i+1 << ": ";
cin.getline((table[i].grade), 30);
cout << endl;
}
cout << setw(20) <<
"ID" << setw(20) <<
"Name" << setw(20) <<
"Grade" << endl;
// Again we use the subscript to get at each student struct
for (int i=0; i<5; i++)
cout << setw(20) <<
table[i].student_id << setw(20) <<
table[i].name << setw(20) <<
table[i].grade << endl;
cout << endl;
// Pass the array of "school" structs
selection_sort(table, 5);
// Show that they have now been sorted
for (int i=0; i<5; i++)
cout << setw(20) <<
table[i].student_id << setw(20) <<
table[i].name << setw(20) <<
table[i].grade << endl;
return 0;
}
// Takes an array of school structs, not integers
void selection_sort(struct school a[], int arrayLength)
{
for (int count = 0; count < arrayLength - 1; count++)
swap(a[count],a[minimum_from(a,count,arrayLength)]);
}
// Again passes school structs, not integer array
int minimum_from(struct school a[], int position, int arrayLength)
{
int min_index = position;
for (int count = position + 1; count < arrayLength; count++)
// Here we compare the student's id against one another. We don't compare the structs directly
// because we haven't defined operators for the struct. How is the compiler suppose to know how to compare?
if (a[count].student_id < a[min_index].student_id)
min_index = count;
return min_index;
}
// Again we are using school structs, not integers. Temp must also be defined to hold a school struct.
void swap(struct school& first, struct school& second)
{
school temp = first;
first = second;
second = temp;
}
As you read through be sure to take notice of my changes (oh and btw for loops don't use a semicolon at the end). Once you read through the comments you should have a better idea of what is going on.
Enjoy!
"At DIC we be student comparing code ninjas.... DIC ninja > student = true every time"