C++中gets()函数未被识别的问题及带空格字符串输入解决方案咨询
gets() Isn't Working (and Better Alternatives for Inputting Strings With Spaces) Hey there! Let's break down what's going on here and get you set up with a safe, working way to input strings that include spaces.
First off: your guess is exactly right. The gets() function was completely removed from C++ in the C17 standard, and it was marked as deprecated (meaning you shouldn't use it) way back in C11. Even if you include <cstdio>, modern compilers won't recognize it anymore—and that's a good thing! gets() is notoriously unsafe: it doesn't check how long the input is, so if someone enters more characters than your str[100] can hold, it'll overflow the buffer and cause all kinds of messy issues (crashes, corrupted data, etc.).
Let's look at two solid, safe replacements:
Option 1: Use cin.getline() for C-style Char Arrays
If you want to keep using a fixed-size char array like your original code, cin.getline() is the perfect drop-in replacement. It lets you specify the maximum number of characters to read, so you avoid buffer overflow:
#include <iostream> using namespace std; int main() { char str[100]; cout << "Enter a string: "; // Reads up to 99 characters (saves one spot for the null terminator '\0') cin.getline(str, sizeof(str)); cout << "You entered: " << str; return 0; }
The cin.getline(str, sizeof(str)) call reads input until it hits a newline or reaches the maximum length we set (99 characters here, since our array is 100 bytes total).
Option 2: Use std::getline() with std::string (Highly Recommended)
For C++ code, using std::string is way cleaner and safer than raw char arrays—it handles memory automatically, so you don't have to worry about fixed sizes or overflow. Here's how to use it:
#include <iostream> #include <string> // Don't forget this header for std::string! using namespace std; int main() { string str; cout << "Enter a string: "; // Reads the entire line, spaces and all, until the user hits enter getline(cin, str); cout << "You entered: " << str; return 0; }
This version has no fixed input limit—the string will grow to fit whatever the user types (as long as there's enough memory, which won't be an issue for normal use).
Quick Heads-Up: Mixing cin >> and getline()
If you ever use cin >> to read something (like a number) before using getline(), you'll need to clear the newline character left in the input buffer. Otherwise, getline() will just read that empty newline and skip your input. Here's how to fix that:
#include <iostream> #include <string> #include <limits> // Needed for numeric_limits using namespace std; int main() { int age; cout << "Enter your age: "; cin >> age; // Clear the newline character left in the input buffer cin.ignore(numeric_limits<streamsize>::max(), '\n'); string name; cout << "Enter your full name: "; getline(cin, name); cout << "Hello, " << name << "! You're " << age << " years old."; return 0; }
That's all you need to know to safely input strings with spaces in C++ now!
内容的提问来源于stack exchange,提问作者dakshSharma

