The code is written poorly, you need to decide exactly what you are writing - C, C + or C ++ (C + is such a joke. When the code is written in C ++, but as much as possible in C style).
why does the error give at the distance of troechnik
Look at your code
for(i=0; i<n; i++) for(j=0; j<4; j++) if(box[i].ocenka[j] == 3) delete [] box[i];
If the very first assessment is a troika, then when checking the second assessment there will be a mess. Because the item is already deleted. And this is wrong. But the problem is that you cannot delete it from a regular array. This is not a vector. But if you want to remove all non-troeshnikov, it is not necessary to delete. You can just skip the output. In your case it will be easier.
Well, I would like to hear what at once shoals are evident.
The worst thing that catches is the following lines.
#include <Windows.h> #include <conio.h>
The first is definitely not needed, and the second is for ancient compilers.
I corrected the code a bit, it is now compiled with a clean C compiler.
#include <string.h> #include <stdlib.h> #include <stdio.h> const int n=2; struct ZACHETKA { char last_name[30]; int no; int ocenka[3]; }; int main() { /* Фамилия студента Номер зачетки Массив из 4 оценок Сортировка структур по фамилии или среднему баллу Удаление всех зачеток троечников */ int j,i; struct ZACHETKA* box = (struct ZACHETKA*)malloc(sizeof(struct ZACHETKA) * n); struct ZACHETKA tmp; for(i=0; i<n; i++) { puts("Vvedite Familiy\n"); scanf("%s", box[i].last_name); puts("Vvedite nomer\n"); scanf("%d", &box[i].no); puts("Vvedite ocenki (3)\n"); scanf("%d %d %d", &box[i].ocenka[0], &box[i].ocenka[1], &box[i].ocenka[2]); } //- Cортировка по фамилии for(i=n-1; i>0 ; i--) for(j=0; j<i ; j++) if(strcmp( box[ j ].last_name, box[ j+1 ].last_name ) > 0) { tmp = box[ j ]; box[ j ]= box[ j+1 ]; box[ j+1 ]= tmp; } for(i=0; i<n; i++) { for (int j = 0; j < 3; j++) { if (box[i].ocenka[j] == 3) goto fin; // да, в данном случае это оправдано, это си } printf("%s\n", box[i].last_name); printf("%d\n", box[i].no); for(j=0; j<3; j++) printf("%d ", box[i].ocenka[j]); printf("\n"); fin:; } free(box); return 0; }
I decided to comb a little code and translate it into a more flattened one. I would say it with a plus and a half.
#include <string> #include <iostream> #include <vector> const int max_number_of_students=2; const int grades_count = 3; struct RecordBook // да, так будет зачетка { std::string last_name; int no; int grades[grades_count]; // раньше это называлось "ocenka" }; int main() { /* Фамилия студента Номер зачетки Массив из 4 оценок Сортировка структур по фамилии или среднему баллу Удаление всех зачеток троечников */ std::vector<RecordBook> box; RecordBook t; for(int i = 0; i < max_number_of_students; i++) { std::cout << "Enter Last Name:" << std::endl; getline(std::cin, t.last_name); std::cout << "Enter Nomer:" << std::endl; std::cin >> t.no; std::cout << "Enter grades (count:"<< grades_count <<")" << std::endl; for (int j = 0; j < grades_count; j++) std::cin >> t.grades[j]; // почему эта строка ниже - http://modwind.ru/publ/cpp_io_input_output_advice/6-1-0-7 std::cin.ignore(50, '\n'); box.push_back(t); } //- Cортировка по фамилии // Здесь хорошо бы применить стандартный алгоритм сортировки, но боюсь это будет перебор. for(size_t i=box.size()-1; i>0 ; i--) for(size_t j=0; j<i ; j++) if(box[ j ].last_name > box[ j+1 ].last_name) { std::swap(box[j], box[j+1]); // три строки ниже - олд-скул вариант. //RecordBook t = box[j]; //box[j] = box[j+1]; //box[j+1] = t; } for(size_t i=0; i<box.size(); i++) { bool is_find = false; for (int j = 0; j < grades_count; j++) { if (box[i].grades[j] == 3) { is_find = true; break; } } if (is_find) continue; std::cout << box[i].last_name << std::endl << box[i].no << std::endl; for(int j=0; j<grades_count; j++) std::cout << box[i].grades[j] << " "; std::cout << std::endl; } return 0; }
The code is a little combed, the transliteration is altered into more or less English, many magic numbers are rendered into constants. The number of students you need to enter is now set only in one place and this is easy to change. I didn’t turn on many changes that I would have done (I would then write another 20 pages of decryption for the teacher).
delete
to delete an element of an array. Instead, you just need to “wipe” the records of troechnik, shifting the rest to the left. Suppose foundk
troechnikov. As a result, you should have actualnk
elements in the array, i.e. then you need to print notn
, butnk
lines. (The size of the allocated memory will not change). - By the way, the search for troechnikov and the transfer of actual records to the left, can be combined in one cycle. Perhaps this is what the teacher wants from you. - avp