我是下面提到的程序:
string s;
cout<<"Enter a string:";
gets(s);
我希望输入的格式为:“希尔顿酒店”。
在使用 gets 时,出现以下错误:
error: cannot convert ‘std::string {aka std::basic_string<char>}’ to ‘char*’ for argument ‘1’ to ‘char* gets(char*)’
我不能使用“cin”,因为我希望我的输入包含空格和特殊字符,如“_”等,我还希望我的分隔符是“enter”。有没有其他方法……或者请您更正错误。
最佳答案
gets
不是很 C++(这是为了与 C 兼容)。使用这个:
#include <iostream>
#include <string>
int main()
{
std::getline(std::cin, s);
std::cout << s << '\n';
}
另外,看看 std::ios::skipws
和 <iomanip>
用于空白处理选项
关于c++ - 获取错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11262313/