Program doesn’t wait for user input with scanf(“%c”,&yn);

printf("Please enter an output filename: ");    
scanf("%s",&outfilename);

When you enter the second string and hit the ENTER key, a string and a character are placed in the input buffer, they are namely: the entered string and the newline character.The string gets consumed by the scanf but the newline remains in the input buffer.

Further,

scanf("%c",&yn);

Your next scanf for reading the character just reads/consumes the newline and hence never waits for user input.

Solution is to consume the extra newline by using:

scanf(" %c", &yn);
      ^^^   <------------Note the space

Or by using getchar()

You may want to check out my answer here for a detailed step by step explanation of the problem.

Leave a Comment