fgets not getting user input

This is a discussion on fgets not getting user input within the C Programming forums, part of the General Programming Boards category; fgets does not want to get the input from the keyboard. What I have been reading on the forums is ...

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    3

    fgets not getting user input

    fgets does not want to get the input from the keyboard. What I have been reading on the forums is that I need to flush the buffer. Either I haven't been able to figure that out or that is not what I need.

    Code:
          printf("Enter an exact name to search for a title:");
    
          fgets(bookName, MAXDATANAME, stdin);\
    In the FAQ, I saw this:

    Code:
      puts("Flushing input");
      
      while ((ch = getchar()) != '\n' && ch != EOF);
      
      printf ("Enter some text: ");
      
      (fgets(buf, sizeof(buf), stdin))
    But I am not exactly sure what ch and getchar do. So can someone help me figure out how to get fgets read from the keyboard?

    Thanx

  2. #2
    CSharpener vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,484
    Quote Originally Posted by golgotha View Post
    Code:
      fgets(buf, sizeof(buf), stdin);
    So can someone help me figure out how to get fgets read from the keyboard?
    red part makes fgets read from keyboard, why do you think it is not working?
    The first 90% of a project takes 90% of the time,
    the last 10% takes the other 90% of the time.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    I don't think it is working because it is reading a \n of EOF that I have not put in.

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,794
    Quote Originally Posted by golgotha
    I don't think it is working because it is reading a \n of EOF that I have not put in.
    If you are talking about the '\n' at the end of the input read, then you did put it in, by pressing enter. You can remove it, of course, perhaps by using strchr() to find '\n' and then replacing it with '\0'.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    3
    I finally got it to work! Thanks for your guys help.
    This is what I added:

    Code:
    do{
       fgets(bookName, MAXDATANAME, stdin);
    }while(bookName[0] == '\n');

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  2. Structure and Linked List User Input Question
    By kevndale79 in forum C Programming
    Replies: 16
    Last Post: 10-05-2006, 11:09 AM
  3. SSH Hacker Activity!! AAHHH!!
    By Kleid-0 in forum A Brief History of Cprogramming.com
    Replies: 15
    Last Post: 03-06-2005, 02:53 PM
  4. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 09:23 AM
  5. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 09:53 AM

Tags for this Thread


1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21