首页 文章

为什么scanf没有接受任何输入并在hackerearth编辑器和代码块中给出错误

提问于
浏览
-3

我想用空格输入字符串输入 . 这是我的代码:

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

using namespace std;

int main()
{
    string name;

    printf("\nEnter the name : ");


    scanf ("%[^\n]%*c", name); //it does not work

    //getline(cin, name); //it works fine

    cout<<name<<endl;

    return 0;
}

hackerearth编辑器和代码块上的错误按摩:

在函数'int main()'中:16:29:错误:无法通过'...'传递非平凡可复制类型'std :: string '的对象

16:29:警告:格式'%[^:string '[ - Wformat =] 16:30:警告:忽略'int scanf(const char *,...)'的返回值,声明属性warn_unused_result [-Wunused-result]

1 回答

  • 0

    string ,或实际上 std::string ,是20世纪90年代的一种C型(ish) .

    scanf 是1066的C函数 . 它不知道如何处理C类型 .

    不要混搭;使用C功能或使用C功能 .

相关问题