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.

    2 answers 2

    The fact is that if scanf could not recognize the number (in your case), then it will not read a single character from the input string. And it will be repeated many, many times.

    How to solve this problem? There are three approaches.

    1) If you could not read, deduct everything to the end and try again.

    scanf ("% d", & casenum);

     while ( casenum != 1 ){ printf("Ошибка! Введено символ. Введите еще раз.\n"); int c; while((c = getchar()) != '\n' && c != EOF); scanf("%d", &casenum); } 

    2) the second way is to read the entire line at once (using fgets), and then parse it with sscanf ( here ).

    3) the third is to read slyly. like this scanf("%d%s", &casenum, buf) . The buf will read the tail of the string. In most cases, it can be ignored, but it can also be analyzed.

    • There should be a check whether the entered value is a digit or a character, if the entered value is a digit, then the program continues to the robot further, if by a symbol, the program allows you to enter a digit until the user enters it. - Vladimir
    • The fact is that later in the code I have a select statement: 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 ("We have entered a no-team command! \ n"); break; } - Vladimir

    Platform-independent solution for C and C ++ languages ​​you will not find these languages ​​are not considered an overflow type of error and there are no exceptional situations for this. The only way out is to write your platform-specific handler in assembler, in the processor for this there is an overflow flag in the register of flags, that's what you can use.

    use read () functions getc () fgets () check the entered array, and then convert the int to char using stroul ()

    • There is not a word about type overflow in the question, actually ... And yet - platform - independent ... in assembler is five! :) - Harry