c++stringgccg++geany

why g++ shows "gets()" not declared ,even after including <cstdio>


#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;

int main()
{
    char str[30];
    gets(str);
}

when i use gets () function compiler gives me the following error

error: 'gets' was not declared in this scope

i was using G++ with geany ide

please make the solution simple cuz iam a beginner.


Solution

  • gets was deprecated in C++11 and removed from C++14. If you are using GCC6.0 or newer then by default it uses C++14 and won't be available. Instead of using

    main()
    {
        char str[30];
        gets(str);
    }
    

    use

    int main()
    {
        std::string str;
        std::getline(cin, str);
    }