"在此范围内未声明gets()"错误

时间:2016-02-07 05:47:25

标签: c++

使用以下代码,我得到"在此范围内未声明gets()"错误:

#include <iostream>
#include <string.h>
using namespace std;

int main()
{

   // string str[]={"I am a boy"};

   string str[20];`

   gets(str);

   cout<<*str;

   return 0;
}

2 个答案:

答案 0 :(得分:5)

函数std::gets()在C ++ 11中已弃用,并已从C ++ 14中完全删除。

答案 1 :(得分:1)

As gets() is a C style function, so if you need to include it in your c++ code then you need to include the header file called stdio.h and moreover you can only pass a c style string to gets() function not c++ string class. So after slight modification in your code it becomes:

#include <iostream>
#include <string.h>
#include "stdio.h"
using namespace std;

int main()
{

// string str[]={"I am a boy"};

char str[20];`

 gets(str);

 printf("%s",str);

return 0;
}