Hello I've been wondering how to get past the whole "program crashes when user inputs the wrong data type". After extensive amount of google searches I found out that you could use !() as an argument in a loop(or if statement) to avoid that. However by doing that I encountered another problem that I couldn't find the solution for.
When I do as the code bellow then it'll just keep going in an eternal loop. And if I was to throw in a new scanf statement inside it the program would then crash.
while(!(scanf("%d",&amount)))
{
printf("some explenation of what went wrong");
}
If I try to use an if statement in a loop to simulate the same(such as the code bellow) then it also would be in an eternal loop even with an alternative scanf. I found something called fflush(0) which didn't work either(I was in the impression that cin.clear() fixed this in c++, and that fflush was C's equal to c++).
while(1)
{
if(!(scanf("%d",&amount)))
{
printf("some explenation of what went wrong");
}
}
So my question is, how can I make the program not crash on faulty data type and not end up in an eternal loop?