Thread: looking at the string of characters in buffer for fgets

  1. #31
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    I might write it like this:
    Code:
    static int get_player_name(Hand *player)
    {
        char line[sizeof(player->player_name) + 1]; // +1 for newline
        if (!fgets(line, sizeof(line), stdin))
        {
            // maybe the user triggered EOF or something, so for our purposes, bail out:
            fprintf(stderr, "Error: unexpected end-of-file or read failure\n");
            exit(EXIT_FAILURE);
        }
    
        char *newline = strchr(line, '\n');
        if (!newline)
        {
            printf("Incorrect Entry: name must have a length of no more than %zu characters\n",
                   sizeof(player->player_name) - 1);
            flush_line();
            return 0;
        }
    
        if (newline == line)
        {
            printf("Incorrect Entry: name cannot be blank\n");
            return 0;
        }
    
        *newline = '\0';
        strcpy(player->player_name, line);
        return 1;
    }
    
    void get_player_names(int num_of_players, Hand players[])
    {
        for (int i = 0; i < num_of_players; i++)
        {
            do
            {
                printf("Please enter player %d's name: ", i + 1);
            }
            while (!get_player_name(players + i));
        }
    
        // this is ok for debugging, otherwise it should not be here:
        for (int i = 0; i < num_of_players; i++)
        {
            printf("Player %d's name is %s\n", i, players[i].player_name);
        }
    }
    Last edited by laserlight; 05-13-2019 at 06:04 AM.
    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

  2. #32
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    wow thank you so much you thought of everything and its so much more concise than my effort above
    thanks
    coop

  3. #33
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Unfortunately, while restructuring my code I removed a crucial line, and didn't notice the omission because I wasn't testing the code. Basically, the original idea behind the strchr was to remove the newline, but I left that out. So, immediately before the strcpy call, you need to replace the newline character with a null character. I have updated my code accordingly.
    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

  4. #34
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    thanks for the update it all works great. dumb dumb here forgot to add get_player_name to the list of function prototypes but it still ran ok is that because it was being called by a function below it?
    coop

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why is fgets not fully populating its buffer?
    By ImageJPEG in forum C Programming
    Replies: 6
    Last Post: 12-27-2017, 09:18 AM
  2. fgets skipping, buffer issue?
    By TLW in forum C Programming
    Replies: 2
    Last Post: 02-20-2011, 08:15 PM
  3. Excess buffer using fgets
    By $l4xklynx in forum C Programming
    Replies: 20
    Last Post: 06-25-2009, 03:56 AM
  4. fgets buffer
    By Pythonsnake in forum C Programming
    Replies: 8
    Last Post: 01-11-2006, 12:27 AM
  5. fgets(buffer,sizeof(buffer),stdin);
    By linuxdude in forum C Programming
    Replies: 2
    Last Post: 10-28-2003, 10:41 AM

Tags for this Thread