Thread: fgets() for stdin not working

  1. #1
    Registered User
    Join Date
    Sep 2003
    Posts
    224

    fgets() for stdin not working

    Hello,
    For the following code snipet:
    Code:
      char src, dst[20];
    
      while(1)
      {
        printf("Source host: ");
        scanf("%c", &src); // (1)
    
        printf("Destination IP address: "); // (2)
        fflush(stdout);
        fgets(dst, sizeof(dst), stdin); // (3)
        *strchr(dst, '\n') = 0;
        strcpy(user_input.dst, dst);
    
        printf("Don't fragement (0, 1)? "); (4)
        scanf("%i", &user_input.df); // (5)
    (1) executes but then the program immediately jumps to (5). (2) and (4) are printed on the same line. What's wrong?

    Thanks

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    Look through this thread for your answer:

    http://cboard.cprogramming.com/showthread.php?t=62611
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    224
    Thanks.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 05-07-2009, 11:31 AM
  2. Using fgets?
    By cogeek in forum C Programming
    Replies: 3
    Last Post: 12-08-2004, 12:08 PM
  3. simple fgets question
    By theweirdo in forum C Programming
    Replies: 7
    Last Post: 01-27-2002, 06:58 PM
  4. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 08:18 PM
  5. fgets & storing
    By hyaline in forum C Programming
    Replies: 1
    Last Post: 09-18-2001, 11:57 AM