#include <cstdio> in Visual Studio 2015 Community Edition

I am an extreme Newbie to C++ Programming, so please "be gentle with me" !!!
I am following a book "C++ A beginner's Guide" and have been doing some examples on a linux Mint machine and it seemed to be going fine. I now have tried migrating the same source code to Visual Studio 2015 Community Edition on a Windows 7.1 PC and i seem to be having very basic problem with #include statements.
Here is the current code that I am scratching my head over- VERY basic stuff!!!

/* Simple program to get a string from the User and echo it to the console. */

#include "stdafx.h"
#include <iostream>
#include <cstdio>
using namespace std;

int main() {
char str[80];
cout << "Enter a string terminated by Enter:\n";
gets(str);
// get a string entered by the user
cout << "\nThe entered string was : " << str << "\n";

return(0);
}

It fails to compile with error message:
1>------ Build started: Project: get_string, Configuration: Debug Win32 ------
1> get_string.cpp
1>\\jlmmds01\redirectedfolders\jlmallard\my documents\visual studio 2015\projects\get_string\get_string\get_string.cpp(11): error C3861: 'gets': identifier not found
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

If I comment out the line #include <cstdio> , i get the same result, so it's like it cannot retrieve the function "gets" out of the header file!

The initial #include "stdafx.h" seems to be required by the Visual Studio 2015 environment, so that is why i have included it.

What on earth am I doing wrong??!!
gets is a deprecated function and no longer exists in the latest C or C++ standards. Why are you trying to use such a dangerous function?

If that book told you to use that function, you should immediately return that book. There are bad C++ books out there and you may have gotten one by mistake. I recommend picking a book from this list:
http://stackoverflow.com/a/388282/1959975
wow!!
Ok- thanks very much for that! The author did say that there was a better way of getting strings, but I guess that what I have unwittingly stumbled upon is different compilers having different tolerances to deprecated fuctions ??
i.e. my Linux Mint compiler made no complaints to gets, but Visual Studio 2015 just completely refuses to use it.

I was feeling rather negative about Visual Studio as being too "weighty", but perhaps I should stick with it if it does better up-front checks?

Thanks again for sharing your expertise.
It's good to use multiple compilers - each compiler has slightly different handling of the C++ language: some are missing features, other have features that are not officially in C++ (called extensions). Your aim should be to write C++ code that works in the most cases.

On Windows I use Visual Studio 2015, nuwen MinGW, and LLVM clang. You can also use online compilers like http://ideone.com and http://coliru.stacked-crooked.com/

Most compilers handle deprecated functions the same way: they issue a warning. In your case, gets is beyond deprecated: it is entirely removed as of C11 and C++14. Don't use it.

gets has long been know to be dangerous; it is infamous for causing buffer overruns. Any tutorial, book, or person that tells you to use it, even temporarily, is putting you in extreme danger. Do not trust such sources of information - who knows what other dangerous constructs they are perfectly OK with teaching you?
Last edited on
> my Linux Mint compiler made no complaints to gets

The Linux Mint compiler, by default, accepts code written in a non-standard dialect of C++

If we
a. Enforce standard-conformance (to the extent that the compiler is capable of doing so) with
-std=c++14 -pedantic-errors

and b. make sure that we use names in the standard library which are in the namespace std -
for instance, std::gets() and not ::gets()

the code that compiles cleanly would be, for the most part, correct (portable, conforming) C++ code.

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>
#include <cstdio>

int main() {
    
    char str[80];
    std::cout << "Enter a string terminated by Enter:\n";
    std::gets(str); // *** error: 'gets' is not a member of 'std'
    
    std::cout << "\nThe entered string was : " << str << "\n";
}

http://coliru.stacked-crooked.com/a/026967fe64a70db3
Last edited on
Topic archived. No new replies allowed.