1

These days I am learning strings and arrays in cpp. In my school they taught us to take a string as user input, one have to use gets() but it isn't working on any of the compilers I have. I have already tried using cstdio library, still errors. I know cin.getline() but it is a bit bigger word.

meispi
  • 63
  • 2
  • 6
  • 5
    You don't use `gets()` anywhere. It is not safe. It is no longer a part of the C11 standard C library. See [Why the `gets()` function is too dangerous to be used — ever!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) – Jonathan Leffler Dec 25 '17 at 06:02
  • 1
    Don't ise `gets()`, period. Use `fgets` instead, this is safe function. And if you are using C++, then use C++ methods , like `std::cin` from `iostream`. – Pablo Dec 25 '17 at 06:03
  • @JonathanLeffler ohh! I didn't know that Thnx for ur help – meispi Dec 25 '17 at 06:05
  • 1
    https://www.google.com/search?q=fgets it's not that hard to look for a function in the internet. – Pablo Dec 25 '17 at 06:09
  • 1
    documentation on most C library functions can be found with `man` on the command prompt of a unix-like operating system or a websearch for `man`. Another excellent place to look for information is at cppreference.com. If you find cppreference difficult to read, fall back to cplusplus.com, which is generally easier to read,but sometimes traded that readability for precision and is slightly wrong. If you suspect you've run across one of those slightly wrong cases, use what you learned from cplusplus.com to take another crack at reading cppreference. – user4581301 Dec 25 '17 at 06:19
  • Apologies. In the above comment `man` should read `man ` – user4581301 Dec 25 '17 at 06:28

3 Answers3

1
cin.get() 

is the thing you are looking for. However, I recommend using cin, as that is sufficient to use cin >> . This cin>> can also be used to input numbers, characters, strings etc.

 gets() and puts() 

are commonly used in code golf, although they serve the same function as cin>> and cout<<. I hope this post helps!

QuIcKmAtHs
  • 297
  • 4
  • 18
0

std::cin.get(); is something they teach in beginner classes, hopefully this is the smaller word for getline you were hoping for! :)

In the future, use a program like Visual Studio which has IntelliSense and can list options for you - or just look at the documentation.

The best method would probably be something along these lines:

std::cout << "Input a string: ";
std::string strInput;
std::cin >> strInput;
std::cout << std::endl << "Your string: " << strInput << std::endl;

Good luck!

J. Doe
  • 208
  • 1
  • 4
  • 13
-1

SEE, 'cin' is used to basically take any input of any data type. BUT. When u input a string using cin it makes an assumption that the string terminates after u enter space. So if u enter 'Hello world' It only reads 'Hello'. When u use gets() it also incorporates spaces into ur input. Syntax: gets(stringName) ; Don't forget to include the header file

Parth Pant
  • 1
  • 1
  • 4
  • `cin` is an input stream and has little to do with how you read data from the stream. What is described in the above answer is the behaviour of the `>>` operator. The stream's `getline` method may be a better fit for `gets`, but when using C++ use `std::string` and ``std::getline` unless you are forced not to by some unfortunate circumstances. – user4581301 Dec 25 '17 at 06:26