2
#include <iostream>
using namespace std;

void main(){
    char name[20];
    gets(name);
    cout<<name<<endl;
}

I can't found answer in google, function gets() is C or C++ language function ? Because in university I must use only C++ functions.

Wizard
  • 10,985
  • 38
  • 91
  • 165
  • 10
    From its manpage: "Never use gets(). Because it is impossible to tell without knowing the data in advance how many characters gets() will read, and because gets() will continue to store characters past the end of the buffer, it is extremely dangerous to use. It has been used to break computer security. Use fgets() instead." and "C89, C99, POSIX.1-2001. LSB deprecates gets(). POSIX.1-2008 removes the specification of gets()" – PlasmaHH Nov 29 '11 at 13:48

5 Answers5

4

C functions are subset of c++ functions, but no, you probably don't want gets() in c++ project.

You may consider getline() or operator>> for stream. Did they not tell you anything about it in the university?

Michael Krelin - hacker
  • 138,757
  • 24
  • 193
  • 173
1

gets is a c function

You are probably looking for istream/ostream/fstream and so on.

See for example: http://www.cplusplus.com/reference/iostream/istream/read/

plaisthos
  • 6,255
  • 6
  • 35
  • 63
1

gets is a c function , First link on google for gets . You should probably look at functions in iostream , fstream etc

Gautam
  • 7,868
  • 12
  • 64
  • 105
1

gets() is a C function dating to the 1960's, it does not do bounds checking and is considered dangerous, is has been kept all this years for compatibility and nothing else.

Your code in valid and recommended C++ should be:

#include <iostream>
using namespace std;

int main(){
    // C style NULL terminated string NOT the same as a C++ string datatype 
    //char name[20];
    string name;// C++ string datatype, meant to use with C++ functions and features
    cin >> name;
    cout<<name<<endl;
    return 0;
}

You should avoid to mix C specific features with C++ features as the string datatype/object. There are ways to use both, but as beginner you should stick to one or the other.

My personal recomendation, do C first, adn then transition to C++. Most C++ programmers are bad at pure C, the C language came first, and was used as the foundation environment for C++, but both have grown apart with time in more ways that you can imagine.

So unless you are studying object orientation simultaneosly with C++, all you will do is code in C with a C++ compiler. C++ is also very large in comparison to C. Templates and Object Oriented Programming facilities being the reasons to use C++ in the first place.

Pure C is still great for many things, is small and elegant. Its easier to get proficient at C than C++. C++ has grown to much to be manageable without sticking to a subset of features agreed by any developer team.

RedComet
  • 1,192
  • 5
  • 11
  • 1
    Note, `gets` and `cin >>` do different things; you'll notice a difference the second you try to read a two-word string. `getline(cin, name)` would be the equivalent to get a whole line. – cHao May 06 '13 at 20:14
0

This example won't compile because the header for the gets is cstdlib and its a c function.

v01d
  • 1,457
  • 1
  • 11
  • 22