Тема: Консольна база даних(сортування масиву структур)
Я програміст-початківець. Як посортувати масив структур наприклад за "goals"?
#include <conio.h>
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
#include <cstdlib>
using namespace std;
struct base //ñòðóêòóðà
{
string name;
int years;
string pos;
string club;
int goals;
int assists;
int matches;
float sallary;
}club;
void setData(int MAX);
void clearData();
void pokazData();
int main()
{
int z;
do//ìåíþ âèêîíóºòüñÿ
{
cout<<"----------------------------------------------------------------\n";
cout<<"[1]Add player\n[2]Check players\n[3]Delete info\n[4]Exit\n";
cout<<"----------------------------------------------------------------\n";
cin >> z;
if(z==1)
{
int MAX;
cout<<"How many players do you want to add?";
cin>>MAX;
setData(MAX);//---------------------çâåðåíåííÿ äî ôóíêö³¿ äëÿ ââîäó ³ çáåðåæåííÿ äàíèõ
}
else if(z==2)//âèâåñòè ãðàâö³â ó ôîðì³ òàáëèö³ ç ôàéëó
{
pokazData();
}
else if(z==3)
{
clearData();
}
else if(z==4)//âèõ³ä ç ïðîãðàìè
{
exit(1);
}
}
while(z!=4);
_getch();
return 0;
}
//-----------------------------------------------------------------------------------------------------------------
void setData(int MAX)//ôóíêöÿ äëÿ ââîäó ³ çáåðåæåííÿ äàíèõ
{
int a=0,i;
do//âèêîíóâàòè ïîêè íàæèìàòè '1'
{
cout<<"----------------------------------------------------------------\n";
base *b; // îáÿâëåííÿ âêàç³âíèêà íà ñòðóêòóðó
b=&club;
int i=0;
for (i=0;i<MAX;++i)
{
cout << "Enter name :"<< endl;
cin >>b->name;
cout << "Enter years :" << endl;
cin >>b->years;
cout << "Enter pos :" << endl;
cin >> b->pos;
cout << "Enter previous club :" << endl;
cin >> b->club;
cout << "Enter goals :" << endl;
cin >> b->goals;
cout << "Enter assists :" << endl;
cin >> b->assists;
cout << "Enter matches :" << endl;
cin >> b->matches;
cout << "Enter sallary(millions) :" << endl;
cin >> b->sallary;
cout<<"----------------------------------------------------------------\n";
ofstream team ("RealMadrid.txt",ios::ate|ios::app);//çàïèñ ñòðóêòóðè â ôàéë
team<<b->name <<"\t"<< b->years << "\t" << b->pos <<"\t"<<b->club<< "\t" << b->goals << "\t"<< b->assists << "\t"<<b->matches << "\t"<< b->sallary << "\t"<< endl;
}
cout << "Do you want to add one more player ? Yes - press '1'; No - press'0'\n" << endl;
cin >> a;
}
while(a==1);
system("cls");
}
//-----------------------------------------------------------------------------------------------------------------
void clearData()
{
fstream clear_file("RealMadrid.txt", ios::out);
clear_file.close();
}
//-----------------------------------------------------------------------------------------------------------------
void pokazData()
{
cout<<"----------------------------------------------------------------\n";
cout<<"name\t"<<"years\t"<<"pos\t"<<"club\t"<<"goals\t"<<"assists\t"<<"matches\t"<<"sallary(millions)\n";
cout<<"----------------------------------------------------------------\n";
ifstream team("RealMadrid.txt");
while(team)
{
string str;
getline(team, str);
cout << str << endl;
};
}