Point 1
Do not use fflush( stdin );
, it is undefined behaviour.
Related: from C11
standard docmunet, chapter 7.21.5.2, (emphasis mine)
int fflush(FILE *stream);
If stream
points to an output stream or an update stream in which the most recent operation was not input, the fflush function causes any unwritten data for that stream to be delivered to the host environment to be written to the file; otherwise, the behavior is undefined.
Point 2 (To get the work done which was suppossed to be done by fflush(stdin)
)
Change
scanf("%c", &type);
to
scanf(" %c", &type);
^
|
The leading whitespace ignores whitespace-like characters in the buffer and reads the first non-whitespace character from stdin
.