2

I'm trying to read input from fgets() and the function doesn't wait for output (from keyboard). We were told to use fgets so scanf is not an option here. The fgets function does work when it's used in the main function for some reason but not in an additional function.

Here's the code

char name[21]={0x0}; //tried everything; not assigning value and NULL&\0

    printf("\nName: ");

    //fseek(stdin,0,SEEK_END);
    //rewind(stdin);
    //fflush(stdin);      //these don't help
    printf(stdin);        //for testing purposes, probably pointless
    fgets(nimi, 21, stdin);

The program will skip the fgets() and not wait for input, and later adds some empty string where I want the name string. However name does not get the value NULL because there's an error check. I know there's quite a few people having this problem but I didn't find any help. Most people tell to use scanf or some other function. For the record scanf() will work as expected here. The syntax should be correct since it works in main(). I probably don't even know how stdin actually works so I apologise if the title is silly.

This snippet is directly after a malloc for pointer (if else clause)

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Jareg
  • 155
  • 2
  • 10
  • 8
    _for testing purposes, probably pointless_....no, it is plain wrong.... – LPs Feb 17 '17 at 16:46
  • 2
    You need to clear stdin before using fgets. – Karim Manaouil Feb 17 '17 at 16:48
  • 5
    Are you using `scanf()` prior to this code? (Likely that a the problem) in addition to `printf(stdin);` is faulty. – chux - Reinstate Monica Feb 17 '17 at 16:49
  • 2
    Why the printf there? It expects a string, anything you pass will be interpreted as a string, which means you have an undefined behavior there. Remove it and it should work fine. – Fernando Coelho Feb 17 '17 at 16:53
  • 3
    `stdin` is not ordinarily seekable, so `fseek()` and `rewind()` should never be used on it. `fflush()` is for flushing *output*, not for discarding unwanted input; if, as would be typical, `stdin` is not open for writing, then the `fflush()` call will fail with an error (which you ignore). Even if it were open for writing, flushing the output side would not serve your purpose. In brief, don't `fflush(stdin)`, either. – John Bollinger Feb 17 '17 at 16:53
  • 5
    Your snippet is not sufficient to explain the behavior you describe. Construct a [mcve] demonstrating the problem, and if that exercise does not help you discover the problem for yourself then edit it into your question. – John Bollinger Feb 17 '17 at 16:55
  • 5
    There is probably a newline still stuck in your input buffer from `scanf()`. – RoadRunner Feb 17 '17 at 16:55
  • 1
    "*most people tell to use scanf or some other function.*". It's fine to use `fgets` here, you just have to make sure to clear the input buffer before you use it, which looks like your problem. – RoadRunner Feb 17 '17 at 17:07

1 Answers1

4

People pointed out that there is something in my input buffer and that is correct. My program now works when I include

 while ((int ch = getchar()) != '\n' && ch != EOF);

in my code before fgets(). There was in fact scanf functions before this. I'm eternally grateful to the guys who replied! Sorry if I made myself look like an idiot with the code. This is one of my first C programs so the code might be "a bit" off. Again thanks.

Jareg
  • 155
  • 2
  • 10