scanf("%d", &casenum); while ( casenum != 1 ){ printf("Ошибка! Введено символ. Введите еще раз.\n"); scanf("%d", &casenum); } If a character is entered, the printf function loops while the scanf does not read the value from the keyboard. To compile code I use the terminal in Linux Ubuntu.
How to make it possible to enter a value in case of an error and continue the program?
Full function example.
int menu(void) { int casenum=0, counter = 0; char answerYes[] = "y", ans[10]; do { printf("+----------------------------------+\n"); printf("|0|Вихід |\n"); printf("|1|Ввести дані |\n"); printf("|2|Видалити запис |\n"); printf("|3|Видалити всі дані про місяць |\n"); printf("|4|Відсортувати за спадінням |\n"); printf("|5|Найменші витрати на харчування |\n"); printf("|6|Найбільші витрати на квартпліту |\n"); printf("|7|Витрати більші ніж зарплата |\n"); printf("+----------------------------------+\n"); scanf("%d", &casenum); while( casenum < 0 && casenum > 7 ){ printf("Помилка! Ви ввели символ. Введіть ще раз.\n"); int c; while((c = getchar()) != '\n' && c != EOF); scanf("%d", &casenum); } switch (casenum) { case 0: return 0; break; case 1: add(); counter = 1; break; case 2: deleteAllDataMonth(); counter = 2; break; case 3: sorting(); counter = 3; break; case 4: minFootCosts(); counter = 4; break; case 5: maxCostsForRent(); counter = 5; break; case 6: spendingMoreThanSalary(); counter = 6; break; default: system("cls || clear"); printf("Ви ввели неістуючу команду!\n"); break; } } while (counter == 0); printf("\n\nБажаєте повернутися в меню або вийти з програми? (y/n)\n"); scanf("%s", ans); if (strcmp(ans, answerYes)==0) { system("cls || clear"); menu(); } else return 0; return 0; } Here I corrected the code, but the problem remained. According to the idea, when entering a symbol, it should display an error, but the program just closes.