Hej, piszę program do szkoły który ma być bazą danych studentów, ma w nim być możliwość sortowania studentów alfabetycznie po nazwisku i utworzyłem taką funkcję. Owszem funkcja działa z tym że po drugim jej wywołaniu. Druga część funkcji czyli wyświetlenie na ekran tabeli danych wykonuje się, lecz dopiero podczas drugiego wywołania tabela jest rzeczywiście posortowana. Dodam jeszcze że sortuję nie same nazwiska, ale całą tablice struktury danych tutaj nazwaną "student". Może ktoś ma jakieś sugestie dlaczego tak się dzieje? Z góry dzięki i pozdrawiam ;)
Funkcja sortująca :
void sortuj(Osoba student[], int ile)
{
string buf[20];
for (int i = 0; i < ile; i++)
{
buf[i] = student[i].nazwisko;
}
sort(buf, buf + ile);
for (int i = 0; i < ile; i++)
{
for (int k = 0; k < ile; k++)
{
if (buf[i] == student[k].nazwisko)
{
swap(student[i], student[k]);
}
}
}
cout << "STUDENTOW W BAZIE : " << ile << endl << endl;
cout << left << setw(15) << "NAZWISKO" << left << setw(15) << "IMIE" << left << setw(7) << "WIEK" << left << setw(15) << "NR. ALBUMU" << left << setw(5) << "SREDNIA" << endl << endl;
for (int i = 0; i < ile; i++)
{
transform(student[i].imie.begin(), student[i].imie.end(), student[i].imie.begin(), ::toupper);
transform(student[i].nazwisko.begin(), student[i].nazwisko.end(), student[i].nazwisko.begin(), ::toupper);
cout << left << setw(15) << student[i].nazwisko;
cout << left << setw(15) << student[i].imie;
cout << left << setw(7) << student[i].wiek;
cout << left << setw(15) << student[i].nr_albumu;
cout << left << setw(5) << student[i].srednia << endl;
}
_getch();
system("cls");
}
Wywołanie funkcji sortującej :
int main()
{
Osoba student[20];
int ilosc_studentow;
ilosc_studentow = licznik_studentow();
wczytaj_dane(student, ilosc_studentow);
while (true)
{
...
cout << "L. Wylistuj baze danych" << endl;
...
char wybor;
wybor = _getch();
switch (wybor)
{
...
case 'l': sortuj(student, ilosc_studentow);
break;
...
default: cout << endl << "NIEPRAWIDLOWY PRZYCISK" << endl; _getch(); system("cls");
}
}
}