Hello guys i am writing a c++ code that should do the following:

1- Creating a stage group just like the champions league first round consisting of 4 teams each on has 4 attributes ( points, gf, ga, gd)
2- The user must be asked to enter each team name, how many points does it have, how many gf, ga (gd is automatically calculated when gf and ga are entered gd=gf-ga)
3- I need to create an array of objects. each object has the attribiutes above (team name, pts, gf, ga gd)
4- I need to sort and output the group from the first till the last as following (most pts, most gd, most gf)

Thank you

What compiler are you using?
Is putting the objects in a sortable collection and then converting that to an array an option?

pardon me if i'm silly, What's a stage group?

Is it wrong to just sort the array of objects thrice say using bubble sort, one on based on the pts, next based on gd and similarly gf ?

Edited 4 Years Ago by Marcusflint

i need to sort the teams decreasingly by points and if 2 or more teams have the same number of points the program should check the one with the higher gd to givr him better position zznd same for gf according to gd

We're not going to do your homework for you. You need to show some efforts of your own towards solving this problem.

Btw, the function you need for sorting an array is std::sort. And your multiple sorting criteria is called a lexicographic ordering (e.g. alphabetical ordering is a kind of lexicographic ordering).

thanks guys this is the solution using visual studio 2008

#include<iostream>
#include<cstring>
using std::string;
#include<cstdlib>
#include<fstream>
using std::ifstream;
using std::ofstream;
using namespace std;

//league header
class league
{
private:
    char* name;
    int points;
    int goals_for;
    int goals_against;
public:
    league(char[]="",int=0,int=0,int=0);
    ~league();

    void set_name(char*);
    void set_points(int);
    void set_gf(int);
    void set_ga(int);

    char* get_name()const;
    int get_points()const;
    int get_gf()const;
    int get_ga()const;
    int get_gd()const;

    void print()const;
};

//league implementation
#include<iostream>
#include<cstring>
using namespace std;

league::league(char* n,int p,int gf,int ga)
{
    set_name(n);
    set_points(p);
    set_gf(gf);
    set_ga(ga);
}
league::~league()
{
    delete[]name;
    name=0;
}
void league::set_name(char* n)
{
    int k = strlen(n);
    name = new char[k+1];
    strncpy(name,n,k);
    name[k] = '\0';
}
char* league::get_name()const
{
    return name;
}
void league::set_points(int p)
{
    points=p;
}
int league::get_points()const
{
    return points;
}
void league::set_gf(int gf)
{
    goals_for=gf;
}
int league::get_gf()const
{
    return goals_for;
}
void league::set_ga(int ga)
{
    goals_against=ga;
}
int league::get_ga()const
{
    return goals_against;
}
int league::get_gd()const
{
    return (get_gf()-get_ga());
}
void league::print()const
{
    cout<<"\t"<<name<<"\t"<<points<<"\t"<<goals_for<<"\t"
        <<goals_against<<"\t"<<get_gd()<<endl;
    cout<<"\t------------------------------------------"<<endl;
}

//main
int main()
{
    fstream outfile("table.dat",ios::out);
    if(!outfile)
    {
        cerr<<"File could not be opened"<<endl;
        exit(1);
    }

    league l;

    for(int i=0;i<3;i++)
        outfile.write(reinterpret_cast<const char*>(&l),sizeof(league));

    outfile.close();
    outfile.open("tabel.dat",ios::in);

    char n[20];
    int p;
    int gf;
    int ga;

    league *lea = new league [3];
    cout<<endl;
    int nb;

    cout<<"\tHow many teams in the group: ";
    cout<<"\t";
    cin>>nb;
    cout<<endl;
    cout<<endl;


    for(int z=0;z<nb;z++)
    {
        cout<<"\tEnter Name(no space), Points, GF, GA (seperated by SPACE)"<<endl<<endl;
        cout<<"\t--> ";
        cin>>n>>p>>gf>>ga;
        cout<<endl;

        l.set_name(n);
        l.set_points(p);
        l.set_gf(gf);
        l.set_ga(ga);

        outfile.seekp(z*sizeof(league));
        outfile.write(reinterpret_cast<const char*>(&l),sizeof(league));

        lea[z].set_name(n);
        lea[z].set_points(p);
        lea[z].set_gf(gf);
        lea[z].set_ga(ga);
    }
    cout<<endl;
    outfile.close();

    league temp;
    for(int g=1;g<nb;g++)
    {
        for(int q=0;q<nb-g;q++)
        {
            if((lea[q].get_points()<lea[q+1].get_points()) || 
                ((lea[q].get_gd()<lea[q+1].get_gd())&&(lea[q].get_points()==lea[q+1].get_points())) || 
                ((lea[q].get_gf()<lea[q+1].get_gf())&&(lea[q].get_gd()==lea[q+1].get_gd())&&(lea[q].get_points()==lea[q+1].get_points())))
            {
                temp=lea[q];
                lea[q]=lea[q+1];
                lea[q+1]=temp;
            }
        }
    }

    fstream osfile("sorted_table.txt",ios::out);

    for(int h5=0;h5<nb;h5++)
    {
        osfile<<lea[h5].get_name()<<"\t"
            <<lea[h5].get_points()<<"\t"
            <<lea[h5].get_gf()<<"\t"
            <<lea[h5].get_ga()<<"\t"
            <<lea[h5].get_gd()<<endl;
    }

    osfile.close();

    cout<<"\tName\tPts\tGF\tGA\tGD"<<endl;
    cout<<"\t------------------------------------------"<<endl;
    for(int h=0;h<nb;h++)
        lea[h].print();

    cout<<endl;
    system("pause");
    return 0;
}

Edited 4 Years Ago by Nick Evan: Added CODE tags!

This article has been dead for over six months. Start a new discussion instead.