"gets () was not declared in this scope" error

Using the following code, I get the message "gets () was not declared in this area" 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;
}
+4
source share
2 answers

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

+4
source

How gets () is a C-style function, so if you need to include it in your C ++ code, you need to include the stdio.h header file, and in addition, you can only pass the ac-style string to the gets () function not C + + string class. Therefore, after a small modification of 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;
}
+1
source

Source: https://habr.com/ru/post/1627630/


All Articles