gets is undefined

Jan 13, 2021 at 4:34pm
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 Jan 13, 2021 at 4:35pm
Jan 13, 2021 at 4:41pm
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);

Jan 13, 2021 at 5:19pm
Jan 13, 2021 at 5:44pm
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.