gets for char array does not work

Hello! My project requires defining an array of char and make the user enter them (by gets command), my friends work on visual studio 2015 and this code works for them, but not for me. I use VS 2017


#include <iostream>
#include <stdio.h>

using namespace std;

void main() {
char x[50];
cout << "Type in" << endl;
gets(x);
}
Last edited on
What do you mean by "not [working] for me"? What exactly is wrong?

gets should not be used in the first place. It's unsafe. That's probably what VS17 is complaining about.

Actually, it's been removed from the language as of C++11 standard. So VS 2017 is up-to-date enough, but VS 2015 evidently isn't.

At the very least, use fgets.

But why not use modern C++ strings with std::getline, and not crusty old C stuff?

Why can't you do this?
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <string>

int main() { // main must return int!
    std::string x;
    std::cout << "Type in" << std::endl;
    std::getline(std::cin, x);
    std::cout << "You typed: " << x << std::endl;
}


1
2
3
Type in
asdf 234
You typed: asdf 234
Last edited on
First of all thank you @Ganado . I cannot use it because the professor says that we cannot use string as it will make it easier for us and he wants us to do it the VERY hard way, So we have to input text as char array not as a string. He wants us to use gets specifically and will not accept any other command . The compiler pops up error C3861 'gets': identifier not found and E0020Identifier "gets" is undefined, but even if it is unsafe can you tell me how to use it correctly if you know ?
Thanks :)
I see... well, sorry but I don't know how to get around using "gets" on Visual Studio 2017's compiler. Maybe someone else knows.
https://msdn.microsoft.com/en-us/library/2029ea5f.aspx

My suggestion would be to email your instructor, if possible, and ask them if it's possible to use gets_s as opposed to gets. It's still the "very hard way" and doesn't use modern C++ std::strings.

Is it possible for you to download Visual Studio 2015, or maybe use another compiler, like GCC?
You can download gcc (MinGW) from a place like here https://nuwen.net/mingw.html
and compile your file via
g++ -std=c++98 mycode.cpp -o mycode.exe

Or, you can use online compilers like ideone.com or cpp.sh to see if they'll let you use gets.
For example, if you go to cpp.sh, go to options and select Standard C++98, otherwise gets won't compile.
Last edited on
In VS2015 you need to change the project properties C/C++ -> Advanced -> Compile As to
Compile as C Code (/TC) then it works, might work in VS 2017 as well

Seems VS does not know much about C99 or C11
Last edited on
The compiler pops up error C3861 'gets': identifier not found and E0020Identifier "gets" is undefined, but even if it is unsafe can you tell me how to use it correctly if you know ?

The reason gets() has be "undefined" is because it can never be used safely or correctly. Your instructor is not doing you any favors by insisting that you use the function. I suggest you use fgets() instead since it can be used correctly and safely.

Edit: It's good to see that VS seems to be getting closer to the current C standards, perhaps one day they'll fully support one of the modern C standards.

Edit2: If you're really writing C++ code then you really should be using istream.getline() to retrieve the C-string instead of the C-stdio functions.

Last edited on
> The compiler pops up error C3861 'gets': identifier not found and E0020Identifier "gets" is undefined,
> but even if it is unsafe can you tell me how to use it correctly if you know ?

std::gets() was deprecated in C++11 and removed by C++14.

There is no way to make std::gets() work with the current Visual Studio 2017 compiler;
it does not support for legacy versions of the standard older than C++14.
The available language standard options are: /std:c++14, /std:c++17 and /std:c++latest

We could write our own (safer) gets() and use that instead:

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

using namespace std;

template < std::size_t N > char* gets( char (&cstr)[N] ) {
     
     // http://en.cppreference.com/w/cpp/io/basic_istream/getline
     return std::cin.getline( cstr, N ) ? cstr : nullptr ;
}

// int main() see: http://www.stroustrup.com/bs_faq2.html#void-main
/* void */ int main() {

    char x[50];
    cout << "Type in\n";
    if( gets(x) ) puts(x) ;
}

http://rextester.com/ZGXC59002
Last edited on
Registered users can post here. Sign in or register to post.