Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

"gets() was not declared in this scope" error [duplicate]

Tags:

c++

With the following code, I get the "gets() was not declared in this scope" error:

#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;
}
like image 412
Sharif Avatar asked Feb 07 '16 05:02

Sharif


2 Answers

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;
}
like image 121
kunal_goyal Avatar answered Oct 24 '22 10:10

kunal_goyal


The function std::gets() was deprecated in C++11 and removed completely from C++14.

like image 26
ThomasMcLeod Avatar answered Oct 24 '22 11:10

ThomasMcLeod