gets is undefined

when i debug the following code using MSVS-2019 compiler says that "gets" is undefined. it means that the program is not being able to reach the stdio.h file. How can I remove this problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include<stdio.h>
using namespace std;

int main()
{
	char message[6] = { 'H','E','l','l','o' }, promod[]= { 'H','E','l','l','o','\0'}, nisha[]="promod kumer dey";
	char sumon[30], name[30];
	cout << message[2]<<" "<<promod[1]<<" "<<nisha<<" "<<message<<"\n enter your 1st name=";
	cin >> sumon;
	cout <<sumon<<"\n enter your 2nd name=";
	gets(name);
	cout <<name;
	return 0;

}
Last edited on
gets() is dead (removed from the language due to bad behavior).
In C we currently use fgets().
In C++, however, it is better to use

 
cin.getline(name, sizeof name);

Two reasons not to use gets_s:
1. Although it is a C11 feature, it is optional and therefore not necessarily available in a particular C11 implementation.
2. It is usually preferable to use C++ functions when writing C++.

It should be noted that istream::getline() sets failbit if the delimiter was not encountered before the buffer is full (including the '\0' terminator, of course). It removes the delimiter (if encountered) from the stream but does not include it in the returned string.
Topic archived. No new replies allowed.