Thread: fgets() does not work properly

  1. #1
    Registered User
    Join Date
    Feb 2021
    Posts
    5

    Question fgets() does not work properly

    Hi! I know this has been raised so many times. But no recommendations from the previous questions worked for me...

    I have 2 fgets() in a row:

    Code:
    printf("Please enter title:");
    // Making sure we don't have newline character in the buffer
    while (getchar() != '\n');
    fgets(title, sizeof(title), stdin);
    
    //Getting rid of newline character in the end
    temp_len = strlen(title);
    if (title[temp_len - 1] == '\n') {
      title[temp_len - 1] = 0;
    }
    
    printf("title: |%s|", title);
    
    printf("Please enter author:");
    
    // Making sure we don't have newline character in the buffer
    while (getchar() != '\n');
    fgets(title, sizeof(authors), stdin);
    
    //Getting rid of newline character in the end
    temp_len = strlen(authors);
    if (authors[temp_len - 1] == '\n') {
      authors[temp_len - 1] = 0;
    }
    
    printf("authors: |%s|", authors);
    If this matters, this all happens in a switch.
    And here are the declaratiosn:

    Code:
    char title[100];
    har authors[100];
    I have also tried fflush(stdin), but the result was the same:
    It lets me enter my string, but when I press ENTER it add newline but still requires me to press ENTER again and then it stops, as a result: empty string...

    Thank you!
    Last edited by Salem; 03-09-2021 at 04:26 AM. Reason: Remove crayola

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    This is the problem:
    Code:
    // Making sure we don't have newline character in the buffer
    while (getchar() != '\n');
    fgets reads and stores the newline read (if there is space for it), so unless the line was too long (i.e., there is no newline stored), you should not be trying to remove the newline from the input buffer.
    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
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 05-14-2017, 06:45 AM
  2. qsort() won't work properly..
    By gevni in forum C Programming
    Replies: 5
    Last Post: 03-21-2013, 12:02 PM
  3. Can't get fgets to work properly
    By lonkz in forum C Programming
    Replies: 18
    Last Post: 01-03-2009, 01:43 PM
  4. Can't Get This Program To Work Properly
    By jbyers19 in forum C Programming
    Replies: 5
    Last Post: 03-09-2006, 10:59 PM
  5. fgets is not reading properly
    By joeprogrammer in forum C Programming
    Replies: 5
    Last Post: 02-20-2006, 09:58 AM

Tags for this Thread