0

I am having a sample CPP program which I am trying to compile using g++. But after running the command

g++ -o test.out test.cpp

I am getting error

test.cpp:7:6: error: 'gets' is not a member of 'std'
 std::gets(ch);
      ^~~~
test.cpp:7:6: note: suggested alternative: 'less'
 std::gets(ch);
      ^~~~
      less

When I check the version of g++ using g++ --version, I am getting the below output

g++ (Ubuntu 7.4.0-1ubuntu1~18.04.1) 7.4.0 Copyright (C) 2017 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Below is my code

#include <stdio.h>
#include <iostream>
using namespace std;
int main()
{
    char ch[100];
    std::gets(ch);
    puts(ch);
    return 0;
}

Can somebody please try to help me how to fix this? As of now, it is not possible to change the versions. I need to make it run in this same version if possible.

Thanks in advance.

Arun
  • 3,640
  • 7
  • 44
  • 87
  • `std::gets` has been deprecated(in c++11) and removed(in c++14). – Jason Mar 23 '23 at 05:09
  • [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) – Jason Mar 23 '23 at 05:11
  • Even before `std::gets()` was removed, the code was still wrong, as `` needed to be `` to get the `std` version of `gets()`. Otherwise, you could have dropped the `std::` qualifier to use the global C version from ``, like you did with `puts()` – Remy Lebeau Mar 23 '23 at 05:51

1 Answers1

0

std::gets has been deprecated(in c++11) and removed(in c++14). Thus it won't work with modern c++.

From std::gets:

char* gets( char* str );
      (deprecated in C++11)
      (removed in C++14)

The std::gets() function does not perform bounds checking. Therefore, this function is extremely vulnerable to buffer-overflow attacks. It cannot be used safely (unless the program runs in an environment which restricts what can appear on stdin). For this reason, the function was deprecated in C++11 and removed altogether in C++14. std::fgets() may be used instead.

Jason
  • 36,170
  • 5
  • 26
  • 60