There is a structure, a line in it, in the process of executing the program it is already full, but it cannot be entered from the keyboard, neither scanf nor gets . What am I doing wrong?

struct Record { int numberRecord; char *lastName; char *firstName; int month; int day; char *doctor; }; void rand_likari(struct Record *pointer_numberRecord, int size) { char *doctors[10] = { "Терапевт","Хірург","Окуліст" }; for (int i = 0; i<size; i++) { (*(pointer_numberRecord+i)).doctor= doctors[rand() % 3]; } } int editList(struct Record *pointer_numberRecord, int size) { struct Record *begin_Record = pointer_numberRecord; int number_Record = -1,true_digit=0; while (number_Record != 0) { rewind(stdin); true_digit = 0; pointer_numberRecord= begin_Record; number_Record = -1; printf("Введіть номер запису для редагування, або 0 для виходу:"); scanf("%d", &number_Record); rewind(stdin); if (number_Record == 0) { return(0); } for (int i = 0; i < size; pointer_numberRecord++, i++) { if (pointer_numberRecord->numberRecord == number_Record) { printf("%-3d %s %s %02d.%02d %s \n", pointer_numberRecord->numberRecord, pointer_numberRecord->lastName, pointer_numberRecord->firstName, pointer_numberRecord->month, pointer_numberRecord->day, pointer_numberRecord->doctor); true_digit++; printf("Редагувати #%d\n", pointer_numberRecord->numberRecord); printf("Введіть лікаря:"); scanf("%s", pointer_numberRecord->doctor);//***ввод с строки клавиатуры*** break; } } if(true_digit==0) printf("Неіснуючий номер запису\n"); } } void main() { srand(time(NULL)); SetConsoleCP(1251); SetConsoleOutputCP(1251); int menu = 1; struct Record mas[10], *recordPointer = mas; int size = sizeof(mas) / sizeof(struct Record); createNumber(mas,size); rand_Date(mas, size); randomAvatar(mas, size); rand_likari(mas, size); while (menu != 0) { menu = -1; //system("cls"); printf("Операції з записами:\n1.Вивід на екран\n2.Редагуваня запису за номером\n3.Сортування записів за датою\n4.Сортування записів за алфавітом\n\n0.Вихід\n"); printf("Введіть номер цифру операції:"); scanf("%d", &menu); rewind(stdin); switch (menu) { case 0: exit(0); break; case 1: showMass(mas, size); break; //функция вывода на экран case 2: { while (editList(mas, size) != 0); //функция для ввода }break; default: menu = 3; } } printf("\n"); system("pause"); } 

    1 answer 1

    It’s time to add to the FAQ ... Flush the buffer after entering the menu - after reading the number, the \n ... character remains

    For example,

     scanf( "%*[^\n]%*c" ); 

    I looked closely - I'm sorry, everything is much worse for you!

     scanf("%s", pointer_numberRecord->doctor);//***ввод с строки клавиатуры*** 

    Where do you want to read the line? Place not allocated. Now the pointer points to a literal , which is by definition constant and into which you cannot write anything, for Undefined Behaviour.

    If you hope to overwrite your Therapist lines and others like them, don’t even hope. No, some compiler will do it; the other will collapse. VC ++ will pretend to write without actually writing anything - although, if you cancel the optimization, you can write ... UB to that and UB, that the program crashes only if you are lucky. You were unlucky :(

    • The problem is that I only need to overwrite the value that was given to it in the rand_likari function, through scanf and gets it failed. - Vitaliy Volk
    • See addition to the answer. - Harry