Thread: fgets() does not work properly

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

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