gpt4 book ai didi

c++ - gets() 未在范围内声明

转载 作者:行者123 更新时间:2023-11-30 02:20:51 27 4
gpt4 key购买 nike

我尝试了很多方法,但没有结果。我用过<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);
^

最佳答案

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

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

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

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

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

关于c++ - gets() 未在范围内声明,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49249067/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com