-4
void name(record *el)
{
    char k[100];
    printf("Name: ");
    fgets(k,100,stdin);
    printf("\n");
}

I'm trying to write a function that reads a line from console and then searches for it in a list. I'm having some problem with reading a whole line. The program just skips the fgets line. The same happens if i try with scanf("%[^\n]%*c").

HairyGnome
  • 11
  • 1
  • This is not a program, this is just one function. Provide a complete example along with the sample input that demonstrates the problem. – William Pursell Nov 28 '20 at 20:00
  • @WilliamPursell Not a bad idea, but this question is answered in every elementary C text, perhaps since K&R. The dupe I provided was literally the first search engine result. – TomServo Nov 28 '20 at 20:02

1 Answers1

0

You probably read an integer before calling the function and the '\n' was left on the input stream.

Try calling the function as the first command you do in main and you will see that it works.

If you read a number right before the call with scanf for example, you could add a '\n' in the parameter string before calling the fgets:

  scanf("%d\n", &x);
vmp
  • 2,370
  • 1
  • 13
  • 17