Problem of sorting the ID in int main.
anyone know how to fix the code ?
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

#include<iostream>
#include<string>
#define MAX 6
#define size 10

using namespace std;

struct student {
string name;
int id;
string prog;
double cgpa;
};


class stinfo {
    private:
        student data[size];
        int last;
    public:
        stinfo();
        void add(string name, int id, string prog, double cgpa);
        void selectionSort(int A[]);
        void printAll(int count);

};


int min(int a[], int pos){
    int min=99999;
    int i;
    int c;
    for(i =pos; i<MAX;i++){
        if(a[i]<min){
            min=a[i];
            c=i;
        }
    }
    return c;
}


stinfo :: stinfo()
{
    last = 0 ;
}



void stinfo::add(string name, int id,string prog, double cgpa)
{
    if (last == size) {
    return ;
}
data[last].name = name;
data[last].id = id;
data[last].prog = prog;
data[last].cgpa = cgpa;
++last;
}           // all infomation are in data 


void stinfo::selectionSort(int data[]) 
{

    int i,j, temp;
     for (i=0; i<MAX; i++)
     {
         j = min(data,i);
         if(data[j]<data[i])
         {
            temp = data[j];
            data[j] = data[i];
            data[i] = temp;
         }       
    }
}


void stinfo::printAll(int count)
{
    if (last == size){
        return ;
    }else{

        for (int i =0; i<count; i++)
        {
            cout<<data[i].name<<endl
                <<data[i].id<<endl
                <<data[i].prog<<endl
                <<data[i].cgpa<<endl;
        }

    }
}



int main()
{

    char ans;
    string name1,prog1;
    int id1;
    double cgpa1;
    int count = 0;

    stinfo st;

    do{


    cout <<"A.) Insert NEW Student Record."<<endl
         <<"B.) print all Student Record."<<endl
         <<"C.) Sorting."<<endl;


    cout <<"Please Choice ONE Option : ";
    cin >>ans;
    cout<<endl;

    switch ( ans )
    {
    case 'a':
    case 'A':
        cout <<endl;
        cout <<">>>>> INSERT NEW STUDENT INFORMATION <<<<<"<<endl;

        cout <<"NAME : ";
        cin >>name1;

        cout <<"ID: ";
        cin >>id1;

        cout <<"PROGRAMME : ";
        cin >>prog1;

        cout <<"CGPA: ";
        cin >>cgpa1;

        st.add ( name1,id1,prog1,cgpa1 );

        cout <<endl
             <<"INSERT COMPLETE... "<<endl
             <<endl;
        count ++;
        break;

    case 'b':

        st.printAll(count);
        break;

    case 'c':

    st.selectionSort(st.data);

    for (int i=0; i<MAX; i++)
    cout<<a[i]<<endl;


    break;

        default :
        cout <<"Please Try Again..."<<endl
             <<endl;
    }

    }while ( toupper(ans) !='F' ) ;

        cout <<"EXIT The Program..."<<endl
             <<endl;

    system("pause");
    return 0;

}

line 156: you are passing the wsrong type of data. selectionSort() wants an array of integers, not an asrray of structures. If you are tring to sort the class's data array (line 18) then remove the parameter to selectionSort() as it is not necessary. Just have selectionSort() sort data directly. If you want the array sorted by id then just use that field in the sort algorithm then when it's time to swap just swap the entire structure.

line 159: what is array a[]?

Edited 3 Years Ago by Ancient Dragon

i have no idea how to pass the data.id into the function ...

This question has already been answered. Start a new discussion instead.