Mình đang học phần thao tác với file trong C++, mình muốn sắp xếp như Table bên dưới, nhưng code của mình chạy bị sai, mọi người check giúp mình với.
LASTNAME |
FIRSTNAME |
GRADE |
Lorzano |
Jeff |
78.23 |
Garcia |
Zen |
89.56 |
void sortFirst() {
string sortedFirst;
string firstname[20];
string key;
string col1;
string col2;
string col3;
ifstream sortByFirst;
cout << "\n\n\tSORTED BY FIRST NAME ================\n\n";
sortByFirst.open("student-file.txt");
while(getline(sortByFirst, sortedFirst))
{
istringstream iss(sortedFirst);
iss >> col1 >> col2 >> col3;
for (int i = 0; i<1; i++) {
firstname[i] = col2;
for (int a = 1, b = 0; a < 20; a++) {
key = firstname[a];
b = a-1;
while (b >= 0 && firstname[b] > key) {
firstname[b+1] = firstname[b];
b--;
}
firstname[b+1] = key;
}
}
}
for (int k = 1; k < 20; k++) {
cout << "\n\t" << firstname[k];
}
sortByFirst.close();
}