Good evening! To study, I need to write an archive program that has a template structure by which the array is filled. Those. The structure contains questionnaire questions, and in the array, depending on its dimension, there will be such a number of completed questionnaires. According to the task, you also need to write a function that erases all data from the array. We were told to add a variable of type bool to the template structure, which is Zero if the item is empty and Unit if the item is filled. Those. resetting bool , we need to reset the contents of the array. However, it does not work out for me. The computer does not feel the connection between the value of the bool variable and the fullness of the array. Tell me, please, what is the error and how to fix it? Everything is commented out, I hope it will be easy to understand. Thank you very much for your help !!!

 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> #include <conio.h> #include <cstdlib> typedef struct { char name[7]; int zach; bool FR; } anketa; FILE *fp; anketa mas[1]; void vvod(int, int); //----------- функция ввода void read_ff(int); void delet (int, int);//---------- функция очистки! int print_1(int); int print_all(int); int poisk_f(int, int); int main() { setlocale( LC_ALL,"Russian" ); setlocale( LC_ALL," "); char sim; int i=0; int kol; printf("Maximalnoe kol-vo anket?"); //---------кол-во анкет; размерность массива scanf("%d", &kol); for(int j=0; j<kol; j++) { mas[j].FR=0; //-----------------показываем,что когда массив пуст, FR равна нулю } printf("i=%d", i); for(;;) { printf("\n\4to budem delat??\n"); printf("v - vvod dannyh. Dobavit' odnu novuyu anketu ; \n"); printf("r - 4tenie. Vyvesti na ekran odnu anketu ; \n"); printf("R - vyvesti vse ankety ; \n"); printf("F - naity anketu ; \n"); printf("d - udalit vse ankety\n"); //--------------------ф-я меню Удалить sim = getch(); if(sim=='\n') sim = getch(); if (sim=='v') { vvod(i, kol); i++; } if (sim=='r') {printf ("\vvedite index elementa kotoryi hotite nape4atat"); scanf("%d", &i); print_1(i-1); } // -------распечатать if (sim=='R') {printf ("\n Vash Arhiv:\n"); print_all(kol); } if (sim=='F') { poisk_f(i, kol); } if (sim=='d') { delet(i, kol); //--------------ВЫЗЫВАЕМ ФУНКЦИЮ УДАЛИТЬ!! } if((sim!='R')&&(sim!='d')&&(sim!='r')&&(sim!='v')) {printf ("VVEDITE 4to-to drugoe\n"); } } system("PAUSE"); return 0; } //************ввод данных,заполнение одной анкеты-******** void vvod(int i, int kol) { printf("i=%d, kol=%d", i, kol); { if ((i<kol)&&(mas[i].FR!=1)) // ---------заполняем только тогда,когда индекс не превышает размерность и FR!=1 { printf ("\neiy? "); scanf("%s", &mas[i].name); printf ("\niiia? ca??oee? "); scanf ("%d", &mas[i].zach); fprintf(fp, "%s ", mas[i].name); fprintf(fp, "%d \n", mas[i].zach); read_ff(i); fclose(fp); mas[i].FR=1; //----------------заполнив одну анкету, приваиваем её FR начение 1, показывающее, что она заполнена i++; } else printf("\niao ianoa aey caiene. Auaa?eoa a?oao? iia?aoe?!\n "); } // ********************удаление. функция очистки******************* void delet (int i, int kol) { for(i=0; i<kol; i++) {mas[i].FR=0; //------------для всего массива делаем FR=0 } } void read_ff(int i) { fp=fopen("k.txt", "r"); for (i=0; i<3; i++) { fscanf (fp, "%s", &mas[i].name); fscanf (fp, "%d", &mas[i].zach); } fclose(fp); } int print_1(int k) { printf("eiy: %s, ", mas[k].name); printf("ca??oea: %d \n", mas[k].zach); } int print_all(int m) { for(int i=0; i<m; i++) { print_1(i); } } int poisk_f (int j, int kol) { char n[7]; printf("aaaaeoa eiy ?aeiaaea, aieaoo eioi?iai oioeoa iaeoe "); scanf("%s", n); for(j=0; j<kol; j++) {if (strstr(mas[j].name,n)!=NULL) { print_1(j); } else {printf ("ia iaeaaii ie iaiie caiene"); break; } } } 
  • one
    Next time, paste the code on pastebin :-) with ipapka, honestly, there is no desire to download. - gecube
  • The code is added to the question. - angry

2 answers 2

Since we are talking about C ++, first we need to replace the mas C array with std::vector<anketa> , the dimension of which is much easier to change with resize() calls, and there will be no prerequisites for memory leaks in the future.

Further, in the vvod () function there are such lines:

 fclose(fp); mas[i].FR=1; //Этот флаг не сохраняется в файл!!! 

It turns out that the flag of fullness is never displayed in the file. Anyway, because the file is opened and closed only inside the read_ff function, then writing to it anywhere elsewhere in the program is meaningless.

By the way, ANYWHERE in the program for writing this file does not open! So the archive will always be empty.


Having looked attentively you understand that it is easier to rewrite everything from scratch than to reanimate this code. And go from C to C ++.

    What is striking is the announcement anketa mas[1]; , ie your array always contains 1 element , no matter what you do next.

    It is necessary either to limit the number of profiles from above , that is, to write something in the spirit of anketa mas[MAX_COUNT]; , or allocate memory dynamically: anketa* mas = (anketa*)malloc(kol * sizeof(anketa) . After that, the memory is freed with free(mas);


    True, the question you have is a С++ tag, so I will also write memory allocation in the C++ style: anketa* mas = new anketa[kol]; , and then delete[] mas .

    When I try to search for other possible errors, to be honest, my eyes curled up into a tube, so I admit that this is not the only problem with this code.