c++ - 获取错误

标签 c++

我是下面提到的程序:

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/

相关文章:

c++ - 可以返回兄弟对象的函数的自动返回类型

c++ - 我对 SDL 的简单介绍程序没有构建。为什么我会收到这些链接器错误?

c++ - 相机校准结果 : why it is similar to input?

c++ - 模板模板部分特化失败 : "expected a class template"

c++ - 当左值引用参数和右值引用参数作为相同的转发引用类型传递时,为什么它不起作用?

c++ - 为什么 gcc 提示 "error: type ' intT' of template argument '0' depends on a template parameter”?

c++ - 成员函数指针和继承

c++ - 如何在C++中提取符号前的数字

c++ - 在 C++ 中创建一个可由其成员访问的类

c++ - `x` 的所有元素是否都存在于 `y`(已排序 vector )中?