scanf()函数在while循环中无法正常工作的问题求助
Hey there, sorry to hear you've been stuck on this for 5 hours—let's break this down and get it fixed! From what you've described, the problem is likely rooted in either your custom strcmp implementation or how you're using scanf() (or both). Let's go through the most common issues and fixes step by step.
1. First: Check Your Custom strcmp Function
Since you can't use the standard strcmp(), a bug here is probably the biggest culprit. The standard strcmp() returns 0 when strings are equal, and a non-zero value when they're not. If your implementation gets this reversed, your condition checks will always fail, leading to that frustrating error message.
Example of a Broken strcmp
If your code looks like this, it's backwards:
// Wrong implementation! int my_strcmp(char *s1, char *s2) { while (*s1 && *s2) { if (*s1 != *s2) { return 0; // Returns 0 when strings differ } s1++; s2++; } return 1; // Returns 1 when strings are equal }
When you check if (my_strcmp(cmd, "MRCT") == 0), this will never trigger for matching strings.
Correct Custom strcmp Implementation
Use this instead—this matches the standard strcmp() behavior:
int my_strcmp(const char *s1, const char *s2) { // Loop until we hit a null terminator or differing characters while (*s1 != '\0' && *s2 != '\0' && *s1 == *s2) { s1++; s2++; } // Return the difference between the first differing characters (0 if equal) return (unsigned char)*s1 - (unsigned char)*s2; }
Now, when you check if (my_strcmp(cmd, "MRCT") == 0), it will correctly trigger when the input matches "MRCT".
2. Fixing scanf() Behavior
Even if your strcmp is correct, scanf() can behave unexpectedly due to input buffer issues or incorrect usage.
a. Use the Right Format Specifier
For reading simple commands like "MRCT" (no spaces), use %s—it automatically skips leading whitespace (like leftover newlines from previous inputs) and reads until the next whitespace. Make sure your input buffer is large enough to hold the command plus the null terminator:
char cmd[10]; // Allocate enough space for your commands
b. Check scanf()'s Return Value
scanf() returns the number of items successfully read. If it fails (e.g., due to invalid input), your cmd variable might hold old data, leading to false negatives. Add a check to handle this:
while (1) { printf("Enter command: "); // Check if scanf successfully read one string if (scanf("%s", cmd) != 1) { // Clear the input buffer to avoid infinite loops while (getchar() != '\n'); printf("Invalid input—please try again.\n"); continue; } // Now check the command with your fixed strcmp if (my_strcmp(cmd, "MRCT") == 0) { printf("MRCT command recognized!\n"); // Add your command logic here break; // Or keep looping for more commands } else { printf("My way of using scanf() does not work here.\n"); } }
c. Avoid %[^\n] Unless You Handle Newlines
If you're using scanf("%[^\n]", cmd) to read strings with spaces, remember that this specifier doesn't consume the newline character left in the buffer. The next scanf() will read this newline as an empty input. Fix this by adding getchar() after reading:
scanf("%[^\n]", cmd); getchar(); // Eat the leftover newline
3. Test in Isolation
To narrow down the issue:
- First, test your custom
strcmp()with hardcoded strings to confirm it works:printf("%d\n", my_strcmp("MRCT", "MRCT")); // Should print 0 printf("%d\n", my_strcmp("MRCT", "TEST")); // Should print non-0 - If that works, test
scanf()alone to make sure it's reading input correctly:char cmd[10]; scanf("%s", cmd); printf("You entered: %s\n", cmd); // Verify the input is captured correctly
内容的提问来源于stack exchange,提问作者dud3

