Click here to Skip to main content
15,886,873 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The custom string input function is introduced in "C Primer Plus":
C++
char *s_gets(char * st,int n)
{
    char * ret_val;
    int i=0;
    ret_val=fgets(st,n,stdin);
    if(ret_val)
    {
        while(st[i]!='\0'&&st[i]!='\n')
              i++;
        if(st[i]=='\n')
              st[i]='\0';
        else
            while(getchar()!='\n')
                 continue;
    }
    return ret_val;
}


What I have tried:

Every time a string is output, I will perform a newline to end the output. The newline can already refresh the buffer. I don’t know whether to perform a while loop to refresh the buffer.
Posted
Updated 4-Aug-23 23:15pm
v2

1 solution

You are currently running your while loop in the 's_gets' function to clear the input buffer (stdin) if there are any characters left in it after reading the input string.

When you use 'fgets' to read input, it reads characters from 'stdin' until it reaches the specified limit (n) or encounters a newline character (\n). If the input string is shorter than the specified limit, the remaining characters (including the newline character) will remain in the input buffer (stdin).

If you don't clear the input buffer and perform a newline immediately after reading the input string, any subsequent input functions (e.g., 'scanf', 'fgets', etc.) might read the leftover characters (including the newline character) in the buffer instead of waiting for new user input. This could lead to unexpected behavior and incorrect readings.

Your code for the 's_gets' function is generally correct and functional, I will however add some improvements -
By using a 'for' loop instead of a 'while' loop to make the code more concise and easier to read.
I will limit the input size in the 'fgets' call. You are passing 'n' as the maximum size to read from 'stdin'. You need to ensure that 'n' is not greater than the size of the buffer 'st' to prevent buffer overflow.
If the user enters an empty line (i.e., just presses Enter without any other characters), your current implementation will still return a valid pointer to an empty string, I will add a check to handle an empty input -

C
char *s_gets(char *st, int n) {
    char *ret_val = fgets(st, n, stdin);
    if (ret_val) {
        //Find the newline character or null terminator in your string...
        for (int i = 0; st[i] != '\0' && st[i] != '\n'; i++) {
            //Replace the newline character with null terminator if found...
            if (st[i] == '\n') {
                st[i] = '\0';
                break; //Exit the loop once newline is found and replaced...
            }
        }

        //If the input line is longer than n-1 characters (including the newline),
        //clear the remaining characters in the input buffer...
        if (strlen(st) == n - 1 && st[n - 2] != '\n') {
            int c;
            while ((c = getchar()) != '\n' && c != EOF)
                continue;
        }
    }
    return ret_val;
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900