代码之家  ›  专栏  ›  技术社区  ›  Chandramauli Chakraborty

gets()未在作用域中声明

  •  1
  • Chandramauli Chakraborty  · 技术社区  · 7 年前

    我尝试了很多事情,但都没有结果。我用过 <cstdio> 但没有帮助。 我再次尝试使用fget,但没有得到任何好的结果。我使用了在线编译器 http://rextester.com/l/cpp_online_compiler_gcc

    #include <iostream>
    #include <string.h>
    #include<stdio.h>
    #include<cstdio>
    using namespace std;
    int main() {
        bool check;
        char sntn[100],word[100],test[20];
        gets (word);
        gets (sntn);
        int j=0,count=0,val,k;
        check=true;
        while(check) {
            for(; sntn[j]==32; j++) {
                test[k]=sntn[j];
                k++;
            }
        }
        k=0;
        j+=1;
        val=strcmp(test,word);
        if(val==0)
            count++;
        if(sntn[j]==32) {
            j+=1;
            check=true;
        }
        if(sntn[j]=='\0')
            check=false;
    
        cout<<endl<<count;
        return 0;
    }
    

    错误:

    source_file.cpp: In function ‘int main()’:
    source_file.cpp:9:11: error: ‘gets’ was not declared in this scope
     gets (word);
               ^
    
    1 回复  |  直到 7 年前
        1
  •  4
  •   R Sahu    7 年前

    std::gets() 在C++11中被弃用,在C++14中被删除。联机编译器必须使用C++14或更高版本的语言。

    更重要的是, gets 已知是一个安全问题。即使编译器支持,也不要使用它。使用 std::fgets() 相反

    除非您需要使用 <cstdio> header,有更好的选择 std::string .

    1. std::getline()
    2. std::istream::getline()

    这些链接包含帮助您入门的示例代码。