Thread: fgets not getting user input

  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
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    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?
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  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
    28,413
    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'.
    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
    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, 03:53 PM
  4. vectors and user input
    By Chaplin27 in forum C++ Programming
    Replies: 6
    Last Post: 01-17-2005, 10:23 AM
  5. Nested Structures - User Input
    By shazg2000 in forum C Programming
    Replies: 2
    Last Post: 01-09-2005, 10:53 AM

Tags for this Thread