-1

I am trying to make a mad libs game but every time I test the first two scanf functions work, but the fgets function is not working how it should be. it keeps printing the same things I want it to but it's showing the mad libs text before the user has a chance to type in the fgets input

#include <stdio.h>

#include <stdlib.h>

int main(void) {

  char color[20];

  char COL2[20];

  printf("Enter a color: ");

  scanf("%s", color);

  printf("Enter a another color: ");

  scanf("%s", COL2);

  printf("Enter a celebrity name: \n");

  char celebrity[15];

  fgets(celebrity, 15, stdin);

  printf("Roses are %s", color);

  printf("Violets are %s", COL2);

  return 0;

}
kaylum
  • 13,833
  • 2
  • 22
  • 31

1 Answers1

0
fscanf(stdin, "%15s", celebrity);

It worked on my laptop, so it should be fine. Also fscanf is better and safer usually from what I've heard. Because it checks for end of line.