Results 1 to 5 of 5
  1. #1

    fgets function discrepancy

    I was creating an app in C and it was supposed to ask a user select one of two choices. Either enter a name string or recite the alphabet. When you select the first option, to enter a string and then repeat it out back, it will not work. What happens is that it will ignore the fgets statement that is supposed to pickup the string that you are to enter into the program. Below is the code that I am using for this app.

    ************************************************** **************

  2. #2
    Junior Member
    Join Date
    Apr 2016
    Posts
    0

    fgets function discrepancy

    I've added some [code] tags to your post, to make it easier to read.

    Let me guess... you enter 1 and then instead of pausing and waiting for you to enter your name, it just continues and prints an empty name.

    Your problem is in the way you're calling scanf.

    I've covered this in an older post, so I'll quote myself from there. In that thread, the original poster was trying to read a single character for a menu choice, by using scanf("%c", &choice). You are trying to read an integer, but the problem is exactly the same. Just replace %c with %i in what I've said below. I recommend you click on the link and read the whole post, though, since it contains a much lengthier explanation.

    I wrote:
    scanf takes input by matching it against the format string. Think of it as an input parser, since that's what it is. It matches the input against the format string, and quits when it's matched everything it was asked for (in your case, 1 character), or when the input differs from what it's expecting. If you tell it to match "%c54%c" it's going to expect to read one character, followed by 54, followed by another character. If it finds anything else, it bails out.

    Note that it won't work if you do scanf("%c\n", &choice); (which would seem like the obvious choice). This would be telling scanf to read 1 character, followed by any amount of whitespace. Since scanf keeps reading from the input until it's matched everything it was asked to match, it will not stop when the user presses Enter (since Enter falls under "any amount of whitespace"). It will only stop when it finds something other than whitespace, after that first Enter.

    scanf can be a complicated beast for the novice, as it's not entirely intuitive. You may find it simpler to use a function such as fgets (which reads a whole line of input) and do the processing yourself. You could process the string with sscanf, or just compare it to your allowed choices with strcmp(line, "y\n").

    Do NOT, however, under any circumstance, use gets: that function is a broken design which you should never use in a real program. You cannot use gets in a secure way (search the web for "buffer overflows" and gets if you're curious), so don't get used to it in the first place.

  3. #3
    Junior Member
    Join Date
    Apr 2016
    Posts
    0

    fgets function discrepancy

    Hmm, I read the older post and if I understand it, you put a whitespace or a \n to clear out the buffer to scanf. I did that and that did not fix the problem. For some reason the fgets function is being ignored when the first choice is selected. Is there another reason why fgets is ignored by the program?

  4. #4

    fgets function discrepancy

    Ah yes, sorry, I forgot you were using an fgets after the scanf. My solution would have more sense if it was a scanf after another scanf. That's what comes from posting at 4 am lol.

    Your problem is still related: the scanf is leaving the Enter on the stdin buffer (because it's being told to read an integer, and an Enter is not an integer). When fgets goes in to read, it finds an Enter there, and it returns an empty line.

    You need to clear out that Enter from the input buffer. You could use something like getchar, or you could save yourself the trouble and just use fgets for the first choice too, and process it with sscanf (or atoi, strtol, etc).

  5. #5

    fgets function discrepancy

    Let me try that. I will get back to you with my results. Thank you. Btw, do you mind if I ask what is your occupation? You seem to know a lot about C?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •