I want to write code that checks if the user enters correct input, i.e 1, 2, 3 or 4. Otherwise, the message "input error" is printed. If the user enters a letter for example, since the input variable in the scanf is char type, it works too.
But in the case of multiple characters, I throught about the following solution: I try to enter all the characters into a char array and to check how many members into it. I wrote the following code:
char option;
int countIn;
char inArray[10];
do { //while option!=4
scanf("%c", &option);
while (countIn < 10 && scanf("%c", &option) != -1 && option != '\n') {
inArray[countIn] = option;
countIn++;
}
if (countIn > 1) { option = 10; }
else { option = inArray[0]; }
countIn = 0;
} while (option != '4');
The problem is when I enter 1 for example, the program works well, but for the second loop iteration, the scanf doesn't work and the program does automatically the part 1 again and again.
what did I do wrong?