Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I am asking the user for input on whether they would like to quit the program or not. There are two snippets. One reads input using the scanf() function, while the second reads input using the fgets() function. Using scanf(), the program goes in an infinite loop. Using fgets(), the program performs as intended. Why does scanf() fail, and fgets() work? How can i correct it so that scanf() will work? Here is the code:

First is with scanf()

#include <stdio.h>
#include <string.h>

int main(void)
{

    char yesNo[6];

    printf("Enter [quit] to exit the program, or any key to continue");
    scanf("%s", &yesNo[6]);

    while (strcmp(yesNo,"quit
") != 0)
    {
         printf("Enter [quit] to exit the program, or any to continue");
         scanf("%s", &yesNo[6]);
    }

    return 0;
}

Second is with fgets()

#include <stdio.h>
#include <string.h>

int main(void)
{

    char yesNo[6];

    printf("Enter[quit] to exit the program, or any key to continue: ");
    fgets(yesNo, 6, stdin);

    while (strcmp(yesNo,"quit
") != 0)
    {
         printf("Enter [quit] to exit the program, or any key to continue:");
         fgets(yesNo, 6, stdin);
    }

    return 0;
}
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
199 views
Welcome To Ask or Share your Answers For Others

1 Answer

The difference between scanf("%s") and fgets you've to keep in mind is the way they take in input.

%s instructs scanf to discard all leading whitespace characters and read in all non-whitespace characters until a whitespace character (or EOF). It stores all the non-whitespace characters in its corresponding argument, in this case, yesNo, and then leaves back the last whitespace character back into the standard input stream (stdin). It also NUL-terminates its corresponding argument, in this case yesNo.

fgets reads in all input until a newline character (' ') or until the maximum number of characters to read passed in as the second argument minus one (for the NUL-terminator '') has been read (or until EOF) and all this input, including the , is stored in its first argument, yesNo here, and it is NUL-terminated.

So, if you have scanf("%s", yesNo); with an input of quit , yesNo will contain just quit and the will be left in the stdin. Since the strings "quit" and "quit " aren't the same, strcmp will not return zero and the while loop will continue to loop.

For fgets(yesNo, 6, stdin); with the input quit , yesNo will hold quit and the stdin will be empty. strcmp returns zero as both the strings "quit " and "quit " are equal, and execution comes out of the loop.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...