-2

Possible Duplicate:
how to sort an integer that is declared in the private

I am having trouble on how to sort the int id element in the class, here is where i would put my functions for the class

#include <iostream>
#include <fstream>
#include <string>
using namespace std;

#include "student.h"
#define cap 100

//Student constructor- empty

student::student()
{
   id = 0;
   first = "null";
   last = "null";
   age = 0;
   gpa = 0.0;
}

//Student deconstructor

student::~student()
{

}

bool student::get(istream &in)
{

   in >> id >> first >> last >> age >> gpa;
   return(in.good());
}
void student::put(ostream &out)
{
   out << id << first << last << age << gpa;
}
bool student::operator>(student)
{
    if(student[i].id>student[i+1].id)
        return true;
    else
        return false;
    cout << student[i].id;
}

bool student::operator<(student)
{
    if(student[i].id<student[i+1].id)
        return true;
    else
        return false;
}
void student::sort_array()
{

    int j,temp;
    for(j=0;j<cap-1;j++)
    { 
//if out of position switch the out of align number
    if(student[i].id<student[i+1].id)
        {
            temp =  student[i].id;
            student[i].id = student[i+1].id;
            student[i+1].id = temp;
        }
    }
}

This is my file, and my main code that would display the and call functions

#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;

#include "student.h"
#define cap 100
void main()
{       string s;
    class student student[cap],l;
    int i;
    fstream f;

    i=0;
    cout << "Enter the file name: ";    //Display to enter the file name
    cin >>s;


    f.open(s.data(),ios::in);
    if(!f.is_open()) 
        cout << "could not open file";
    while(i<cap && !f.eof())
    {   
        student[i].get(f);

 //Display if okay
        if(f.good())
        {
            student[i].sort_array();
            i++;
            cout << i;
        }
    }
    f.close();
}

this is my class code, and class file

#include <iostream>
using namespace std;

class student
{
    public:
        student(); //Constructor without parameters
        student(int,string,string,int,float); //Constructor with parameters
        ~student(); //Deconstructors
        bool get(istream &);    //Input
        void put(ostream &);    //Output
        bool operator==(student);
        bool operator>(student);
        bool operator<(student);
        bool operator==(int);
        bool operator>(int);
        bool operator<(int);    
        int read_array(string,int);
        void sort_array();
    private:
        int id,age;
        float gpa;
        string last,first;
 };
Community
  • 1
  • 1
MIkey27
  • 25
  • 1
  • 5
  • You seem to be confusing your class, `student` with a collection of `student`s. – Chowlett May 01 '12 at 13:15
  • 2
    You cannot sort a single element. – juanchopanza May 01 '12 at 13:17
  • @Mikey72: Please do not ask the same question multiple times. If you don't get the answer you are looking for then you should clarify your existing question. http://tinyurl.com/so-hints contains some sage advice for writing the perfect question. – johnsyweb May 01 '12 at 23:48

1 Answers1

0

The common technique is to provide comparison operators, <, >, <=, >=, == and !=, for a class that can be compared. Most sort algorithms require that items in the container can be "less than comparable".

Look up the Boost library as it has tools that will define all comparison operators when only less-than and equality are defined.

In your design, you will need a container for your students. Use std::sort or your preferred sorting function on the container. If this is homework, you may want to put the container in main() along with the sorting logic.

Also, either search the web for "C++ overloading operators" or define your own method. If you are clever, you can design your class to have different comparison functions to allow sorting by the different fields.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154