0
printf("Number of tracks: ");
fflush( stdin );
scanf("%d", &track);

printf("Is this an album or single: ");
fflush( stdin );
scanf("%c", &type);

when I entered 5 for the number of track, the program displays Is this an album or single and ends the program there without letting me enter the album type?

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
max
  • 31
  • 7

1 Answers1

2

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.

Community
  • 1
  • 1
Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261