Thread: Function getting skipped on first loop but not on the rest?

  1. #1
    Registered User
    Join Date
    Aug 2015
    Posts
    26

    Function getting skipped on first loop but not on the rest?

    The fgets function in this loop:
    Code:
    for(int i = 1; i < players + 1; i++)    {
            printf("Enter your name player %d\n", i); //ask for name
            fgets(player_name[i], sizeof(player_name[i]), stdin); //get name
            putchar('\n');
        }
    Is being skipped on the first loop, but on the rest of the loops it runs, any help as to why that happens? the entire file that snippet is from is attached at the bottom.
    school.c
    Last edited by YayIguess; 09-23-2015 at 03:46 PM.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,982
    See the FAQ on correct ways to clean up the input stream after using say scanf()

    Hint: it's not fflush(stdin)
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Aug 2015
    Posts
    26
    Would that be the reason it wasn't working? I shouldn't have included the fflush()s as they were only put there to see if they would make it work, which they one.

    edit: updated OP
    Last edited by YayIguess; 09-23-2015 at 03:46 PM.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    37,982
    > Would that be the reason it wasn't working?
    Yes, exactly.
    fflush(stdin) doesn't work on your system.

    fflush() is only standardised for output/update streams only.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User FourAngels's Avatar
    Join Date
    Aug 2015
    Location
    Canada
    Posts
    130
    player_name[i] is not the first element if i is 1 instead of 0. Maybe that is the problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My setup() function is being skipped.
    By howardbc14 in forum C Programming
    Replies: 3
    Last Post: 05-23-2015, 01:47 PM
  2. My getline function is skipped whenver I use case 10.
    By howardbc14 in forum C++ Programming
    Replies: 2
    Last Post: 05-17-2015, 12:12 AM
  3. My setup() function is skipped.
    By howardbc14 in forum C Programming
    Replies: 3
    Last Post: 05-14-2015, 11:29 PM
  4. gets function skipped 1st time
    By ambrown782 in forum C Programming
    Replies: 6
    Last Post: 07-26-2014, 12:30 AM
  5. Replies: 4
    Last Post: 02-22-2012, 12:05 PM