Need Help with fgets!!

Discussion in 'C' started by Avan, Oct 6, 2011.

  1. Avan

    Avan New Member

    Oct 6, 2011
    Likes Received:
    Trophy Points:
    Hello, I am new here and this is my first post. Was doing some assignment to take in multiple strings inputs but in the meantime, each character in the string is important. So I tried using fgets.

    sample program:

    for(num = 1; num<5; num++)
    printf("Enter 8 characters: ");
    fgets(string, 8, stdin);
    printf("%c\n", string[3]);

    However the output I got is: *blue* my input

    Enter 8 characters: abcdefgh
    Enter 8 characters: d
    Enter 8 characters: *waiting for my input*

    How come the fgets does not wait for my second input during the second loop and just proceed on. Is there a way to solve this or actually there is another solution beside fgets that can solve my problem

    Thanks a lot
  2. gpk kishore

    gpk kishore New Member

    Jun 30, 2011
    Likes Received:
    Trophy Points:
    simply use fflush(stdin); after your fgets statement (or)
    use gets(string) instead of fgets
    I think your problem will be solved
    once check it out

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice