#define MAGIC_NUMBER //place your favourite integer here
#include <stdlib.h>
#include <stdio.h>
int main()
{
int input,i;
for(int i=0;i<100;i++)
{
scanf("%d",&input);
if(input== MAGIC_NUMBER)
printf("%d ",input);
else
printf("Invalid value\n");
}
system("pause");
return 0;
}
This program crashes when a non-integer value is entered. I want to get scanf back to normal work after the character had been scanned.
From scanf man-page:
So you can check the return value and read-in bad values to discard if there is an error: