0
#include <stdio.h>

int main(void) {

    int numWords;
    char str[100];

    printf("Enter how many words:\n");
    scanf("%d", &numWords);

    printf("Enter %d words:", numWords);

    fgets(str, 100, stdin);

    return(0);
}

This program does not read fgets at all. It will print the text and scan what what the user enters into the numWords, but will not read scanf. I have tried putting a newline character after scanf but then the "Enter %d words:" will not print. I would like everything to print in order.

This is just the beginning of a more complex program, not the whole thing.

John Kugelman
  • 349,597
  • 67
  • 533
  • 578
gcasey
  • 1
  • Standard problem: you can't reliably call `fgets` after `scanf`. – Steve Summit Sep 23 '22 at 22:06
  • Some [secret unwritten rules](https://stackoverflow.com/questions/72178518#72178652) for using `scanf` successfully. – Steve Summit Sep 23 '22 at 22:06
  • 2
    There is a newline left after the `scanf` call which causes `fgets` to read a blank line. The usual advice is not to mix `scanf` and `fgets` calls. Use `fgets` to read stdin into a string and `sscanf` to parse fields from the string. – kaylum Sep 23 '22 at 22:06

0 Answers0