Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,450 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,165 people online right now. Registration is fast and FREE... Join Now!




sorting by a member(field) of a struct

 
Reply to this topicStart new topic

sorting by a member(field) of a struct

xpertpan
22 Nov, 2007 - 01:35 PM
Post #1

New D.I.C Head
*

Joined: 22 Nov, 2007
Posts: 9


My Contributions
hi guyz
i have a homework with a lot of thinks to do but i have a proplem in one of this thinkz, sorting by a member(field) of a struct.
(my english is not good..)
we will call function void sorting that will sorting the table in declining line (top bigger-bottom smaller) as for the grade. If two or more students have the same grade, first will be written the student with the smaller ID


i try to do sorting with many wayz
i try to put this value 'table[pass].grade' in a variable
i try to compare this without variable.
table[pass].grade < table[row].grade

but nothing

compiler says to me:
many times this
In function `void sorting(student)':

75 D:\my files\TEI\D cemester\texnikes prog\texn_teu_3\katranitsas_ask3v2.0.cpp no match for 'operator[]' in 'table[pass]'
76 D:\my files\TEI\D cemester\texnikes prog\texn_teu_3\katranitsas_ask3v2.0.cpp no match for 'operator[]' in 'table[row]'
many times this

and this
In int main()':
272 D:\my files\TEI\D cemester\texnikes prog\texn_teu_3\katranitsas_ask3v2.0.cpp conversion from `student*' to non-scalar type `student' requested
this one was written to me at poin tha i call sorting function in main.



CODE

#include<iostream.h>
#include<fstream.h>
#include<string>



struct student{
               char lname[20]; <--its mean lname
               int ID;                <--its mean ID
               char year;             <--year
               int grade;          <--grade
};

void getData(ifstream& file, student* table){
    
     for(int i(0); i<100; i++)
             file>>table[i].lname>>table[i].id
             >>table[i].year>>table[i].grade;
}//end void getData

//that is working, i dont call this function wet
void menu(int& eggrafi, int& epilogi){
     do{
               cout<<"Dose to aritmo tis eggrafis pou theleis epeksergasteis\n"
                   <<"i timi prepei na einai metaksi tou 0 kai tou 99\n"
                   <<"eggrafi : ";
                   cin>>eggrafi;
               cout<<"Dose to aritmo tis epilogis\n"
                   <<"i timi prepei na einai metaksi tou 1 kai tou 5\n"
                   <<"dose 1 gia na ginei swap tis eggrafis 'eggrafi'"
                   <<"tou protou pinaka me tin eggrafi 'eggrafi' tou deuterou \n"
                   <<"dose 2 gia na ginei swap to onoma tis eggrafis 'eggrafi'"
                   <<"tou protou pinaka me to omoma tis eggrafi 'eggrafi' tou "
                   <<"deuterou \n"
                   <<"dose 3 gia na ginei swap tou AEM tis eggrafis 'eggrafi'"
                   <<"tou protou pinaka me ton AEM tis eggrafi 'eggrafi' tou "
                   <<"deuterou \n"
                   <<"dose 4 gia na ginei swap tou etous tis eggrafis 'eggrafi'"
                   <<"tou protou pinaka me to etos tis eggrafi 'eggrafi' tou "
                   <<"deuterou \n"
                   <<"dose 5 gia na ginei swap ton vathmo tis eggrafis 'eggrafi'"
                   <<"tou protou pinaka me ton vathmo tis eggrafi 'eggrafi' tou "
                   <<"deuterou \n\n"<<"epilogi : ";
                   cin>>epilogi;
     }while((eggrafi<0 || eggrafi>99) && (epilogi <1 || epilogi>5));
}//end void menu  



[color=#FF0000]//here is the problem(maybe..)[/color]
void sorting(student table)
{

    student temp_storage;
    int gradePass, gradeRow;
    
    for(int pass(0); pass < 2; pass++)
        for(int row(1); row < 2; row++)
        {
                gradePass=table[pass].grade;
                gradeRow=table[row].grade;
            if(gradePass < gradeRow)
            {
                temp_storage = table[pass];
                table[pass] = table[row];
                table[row]  = temp_storage;
            }
            
            if(table[pass].grade == table[row].grade)
            {
                 if(table[pass].id > table[row].id)
                 {
                      temp_storage = table[pass];
                      table[pass] = table[row];
                      table[row]  = temp_storage;
                 }
    
            }
        }//end for(row
}//end sorting void


//merge function was wrong but if you help me in sorting
//function i solve this alone
void merge(student** table1, student** table2, student** table3){
     int pA,pB,pC;
    
     pA=pB=pC=0;
     while(pA<100 && pB<100)
     {
            if(table1[pA]->vathmos <table2[pB]->vathmos)
            {
                table3[pC] = table2[pB];
                pC++;
            }
            else if(table1[pA]->vathmos >table2[pB]->vathmos)
            {
                table3[pC] = table1[pA];
                pC++;
            }
            else if(table1[pA]->vathmos == table2[pB]->vathmos)
            {
                if(table1[pA]->aem > table2[pB]->aem)
                {
                  table3[pC] = table2[pB];
                  pC++;
                }
                else
                {
                  table3[pC] = table2[pB];
                  pC++;
                }
            }
            if(pA==100)
            {
                       for(int i(pB); i<2;i++)
                       {
                               table3[pC]=table2[pB];
                               pC++;
                       }
            }
            else
                for(int i(pA); i<2; i++)
                {
                        table3[pC]=table1[pA];
                        pC++;
                }
     }//end while
}//end void merge          

main(){
      
       student table1[100];
       student table2[100];
       student table3[200];

      
      
        
       ifstream file1;
       file1.open("students1.dta");
       getData(file1, table1);
       file1.close();
      
       ifstream file2;
       file2.open("students2.dta");
       getData(file2, table2);
       file2.close();
      
       sorting( table1);
       sorting( table2);
        

       system("pause");
      

}//end main



any syntax error are owed to copy-paste and at translating
sssssooooorrrrryyyyy about my english
Please help me out with it
I would really appreciate that !!


User is offlineProfile CardPM
+Quote Post

NickDMax
RE: Sorting By A Member(field) Of A Struct
22 Nov, 2007 - 01:52 PM
Post #2

2B||!2B
Group Icon

Joined: 18 Feb, 2007
Posts: 2,864



Thanked: 53 times
Dream Kudos: 550
My Contributions
you pass a single instance of 'student' and then try to use it as an array. try:
void sorting(student *table)
User is offlineProfile CardPM
+Quote Post

xpertpan
RE: Sorting By A Member(field) Of A Struct
22 Nov, 2007 - 02:08 PM
Post #3

New D.I.C Head
*

Joined: 22 Nov, 2007
Posts: 9


My Contributions
QUOTE(NickDMax @ 22 Nov, 2007 - 02:52 PM) *

you pass a single instance of 'student' and then try to use it as an array. try:
void sorting(student *table)

i'm failure...... lol biggrin.gif
after 5h programming and 6h lesson i have conflicted.....
thank you very mach NickDMax....
i debug my program until here and it's working good
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/7/09 12:59PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month