Thread: scanf being ignored after fgets

  1. #1
    Registered User
    Join Date
    Oct 2019
    Posts
    3

    Question scanf being ignored after fgets

    Hey, all

    I'm new to C and trying to figure out why one of my scanf's are being ignored when running the code.

    Code:
    scanf being ignored after fgets-capture1-jpg

    Let me know if I need to upload the rest of the code!
    I'm sure the issue is white space but I've tried buffering with getchar(); to fix that issue but nothing works!!


    Output:
    scanf being ignored after fgets-capture2-jpg
    Last edited by FreshLemons; 10-27-2019 at 08:41 PM. Reason: wrong title

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Remove the getchar() call after the fgets() call: you're using getchar() to remove the newline left in the input buffer, but fgets already reads and stores that newline (if there's enough space to store it, which is probably a good assumption to make for now).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2019
    Posts
    3
    Thanks for the response! I tested it, but now when it gets to the fgets() call, it also runs the 3rd scanf() so the grades is still not getting its input

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Oh. Does your compiler mention any warning? I notice that your first scanf call has a "d" instead of a "%d".
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2019
    Posts
    3
    I feel so dumb! That was the issue and it fixed everything. I can't believe I didn't see that. Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf and EOF
    By Satya in forum C Programming
    Replies: 3
    Last Post: 03-05-2012, 11:26 AM
  2. Help with while's and scanf's...
    By m600 in forum C Programming
    Replies: 14
    Last Post: 01-19-2010, 03:31 PM
  3. scanf
    By cProgNoob in forum C Programming
    Replies: 4
    Last Post: 12-10-2008, 11:04 AM
  4. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM

Tags for this Thread