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

  1. #1
    Registered User
    Join Date
    Apr 2019
    Posts
    808

    looking at the string of characters in buffer for fgets

    i have the following function that asks the user to input their name. and stores it as one of the members of a struct called player name that can hold upto 10 characters including the \0 character.
    Code:
    void get_player_names(int num_of_players, Hand *p_player)
    {
        int i, x;
        char buff[11];
    
        for (i = 0; i < num_of_players; i++)
        {
            printf("Please enter player %d's name: ", i + 1);
            x = 0;
            while (x != 1)
            {
                fgets(buff, sizeof(buff), stdin);
                flush_line();
                x = sscanf(buff, "%s", p_player[i].player_name);
                if (x != 1)
                {
                    printf("Incorrect Entry\n Please enter player %d's name: ", i + 1);
                    x = 0;
                }
            }
        }
    }
    the issue is i need the buffer to be the same size at least but if i input a shorter name it waits for me to input the remaining characters to fill the buffer.

    is there away i can look at each character as it comes in using fgets and search for the newline feed
    many thanks
    coop

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why do you have the flush_line() call there?
    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

  3. #3
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    because i copied the code from another function that reads in a single character and if 10 were entered it printed out invalid entry 10 times so after it read the first character i discarded the rest as not needed. admittedly i don't need it for this function.

  4. #4
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Let's say that you want to stop when the user types "grape"
    Code:
    if (!strcmp (userInput, "grape"))
        break;
    Fact - Beethoven wrote his first symphony in C

  5. #5
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    now i get the warning passing argument 1 of strcpy makes makes pointer from integer without cast
    Code:
    void get_player_names(int num_of_players, Hand *p_player)
    {
        int i, x, str_index = 0;
        char buff[2], str_temp[10];
    
        for (i = 0; i < num_of_players; i++)
        {
            printf("Please enter player %d's name: ", i + 1);
            x = 0;
            while (x != 1)
            {
                fgets(buff, sizeof(buff), stdin);
                if (!strcmp(buff, "\n"))
                {
                    strcpy(str_temp[str_index], buff[0]);
                    str_index++;
                }
                else
                {
                    x = sscanf(str_temp, "%s", p_player[i].player_name);
                    if (x != 1)
                    {
                        printf("Incorrect Entry\n Please enter player %d's name: ", i + 1);
                        x = 0;
                    }
                }
            }
        }
    }
    what have i done wrong now
    coop
    Last edited by cooper1200; 05-13-2019 at 01:58 AM.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Examine the types of the arguments of strcpy. What are the types of the arguments that strcpy expects and what are the types of the arguments that you passed?

    Basically, do this whenever you see "makes makes pointer from integer without cast". The message is telling you that you were supposed to pass a pointer but didn't.
    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

  7. #7
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i know you must shake your head in disrepair at me i do appreciate your feed back
    thanks
    coop

  8. #8
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    now i get a warning saying initialization from incomparable pointer type.
    Code:
    void get_player_names(int num_of_players, Hand *p_player)
    {
        int i, x, str_index = 0;
        char buff[2], str_temp[10], *p_str_temp = &str_temp;
    
        for (i = 0; i < num_of_players; i++)
        {
            printf("Please enter player %d's name: ", i + 1);
            x = 0;
            while (x != 1)
            {
                fgets(buff, sizeof(buff), stdin);
                if (!strcmp(buff, "\n"))
                {
                    strcpy(p_str_temp[str_index], buff[0]);
                    p_str_index++;
                }
                else
                {
                    x = sscanf(str_temp, "%s", p_player[i].player_name);
                    if (x != 1)
                    {
                        printf("Incorrect Entry\n Please enter player %d's name: ", i + 1);
                        x = 0;
                    }
                }
            }
        }
    }

  9. #9
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Answer my questions in post #6.
    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

  10. #10
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    the next line of the warning takes me to this "extern char *strcpy (char *__restrict __dest, const char *__restrict __src)" so a char pointer right?
    coop

  11. #11
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    however, looking at the rest of that line by that logic it wants a pointer to the buff variable in my case which it isn't complaining about yet

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Yes, but that's only half the answer.

    Look, I asked you to examine what was actually passed for a reason. You took one look at the expected argument type and jumped to a wrong conclusion.

    Here's another one:
    Code:
    void foo(int *p){}
    Given this:
    Code:
    int xs[] = {1, 2};
    Which of the following is correct?
    Code:
    foo(xs[0]); // option A
    foo(xs); // option B
    foo(&xs); // option C
    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

  13. #13
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    i honestly don't know. its one of the issues i really don't get i have used both b and c in the past and both have worked and option a and the only difference with option a is your passing a specific element
    coop
    Last edited by cooper1200; 05-13-2019 at 02:53 AM.

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What is the type of the argument for each of the options?
    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

  15. #15
    Registered User
    Join Date
    Apr 2019
    Posts
    808
    an int

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