why g++ shows “gets()” not declared ,even after including

后端 未结 2 1338
我寻月下人不归
我寻月下人不归 2021-01-28 21:33
#include 
#include 
#include 

using namespace std;

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

when

相关标签:
2条回答
  • 2021-01-28 22:00

    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);
    }
    
    0 讨论(0)
  • 2021-01-28 22:11

    gets is an unsafe function and is not supported by the C Standard any more.

    Instead use fgets.

    For example

    #include <iostream>
    #include <cstdio>
    #include <cstring>
    
    int main()
    {
        char str[30];
    
        std::fgets(str, sizeof( str ), stdin );
    
        str[ std::strcspn( str, "\n" ) ] = '\0';
    
        //...
    }
    
    0 讨论(0)
提交回复
热议问题