编译代码时出错

Zac*_*low 3 c++ printf declaration

我在编译此代码时收到此错误字符串未声明.

#include <stdio.h>
#include <stdlib.h>


int main()
{
string names;
printf("What is your name?\n");
scanf("%s", &names);

printf("Your name is %s", names);
return 0;
}
Run Code Online (Sandbox Code Playgroud)

有人可以告诉我为什么.非常感谢

tao*_*ocp 8

你应该包括字符串标题:

#include <string>
Run Code Online (Sandbox Code Playgroud)

并且std在使用它时不要忘记命名空间:

std::string names;
Run Code Online (Sandbox Code Playgroud)

此外,编码时不要混用C和C++.尝试使用std::coutprintf,cin/getlinescanf.

  • 特别是,将`std :: string`对象(在本例中为名称)作为参数传递给`printf`肯定不会按预期工作. (2认同)