-1

Everyone I'm a beginner for learning Computer Science and now I've already learnt some basic of the language C language.

In the C language we can use fgets to take multiple words as an input. What is the C++ equivalent function to take such type of input?

DevSolar
  • 67,862
  • 21
  • 134
  • 209
  • 1
    Welcome to stackoverflow.com. Please take some time to read [the help pages](http://stackoverflow.com/help), especially the sections named ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) and ["What types of questions should I avoid asking?"](http://stackoverflow.com/help/dont-ask). Also please [take the tour](http://stackoverflow.com/tour) and [read about how to ask good questions](http://stackoverflow.com/help/how-to-ask). Lastly please learn how to create a [Minimal, Complete, and Verifiable Example](http://stackoverflow.com/help/mcve). – Some programmer dude Jul 17 '18 at 07:30
  • 1
    Also please read [this question checklist](https://codeblog.jonskeet.uk/2012/11/24/stack-overflow-question-checklist/), and all of http://idownvotedbecau.se/ to learn some reasons your question might be down-voted. Finally, please [learn how to debug your programs](https://ericlippert.com/2014/03/05/how-to-debug-small-programs/). – Some programmer dude Jul 17 '18 at 07:30
  • 1
    ? I always thought that every C command can be used in C++ too. Isn't it? – Dominique Jul 17 '18 at 07:33
  • 1
    You should not be using `gets` in C either, see: [Why is the gets function so dangerous that it should not be used?](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-so-dangerous-that-it-should-not-be-used) (in fact the C11 standard removed the `gets` function) – UnholySheep Jul 17 '18 at 07:34
  • 2
    C++ doesn't have 'commands'. Please get a C++ book from the [list](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list). Oh and you **cannot** use gets, there's no such function in C. – n. m. could be an AI Jul 17 '18 at 07:34
  • [Here's a list of good books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282), get a couple of them and start reading. – Some programmer dude Jul 17 '18 at 07:38
  • @Dominique: It can, but there are things from C that you really, honestly, genuinely *would not want* to use in C++ if you have any alternative. C-style arrays, strings, and non-type-safe IO being foremost on this list. – DevSolar Jul 17 '18 at 07:42
  • 1
    Possible duplicate -- coming from a different angle but arriving at the same answer: [How do I read long lines from a text file in C++](https://stackoverflow.com/questions/2910836/how-do-i-read-long-lines-from-a-text-file-in-c), or [Read a line of strings from a file](https://stackoverflow.com/questions/49959181/read-a-line-of-strings-from-a-file). Not wanting to dupe-hammer this close as it isn't a 100% match, but putting it out here to see if the non-golds agree. ;-) – DevSolar Jul 17 '18 at 07:46
  • @DevSolar: Personally I think it should stay up, especially given the edits. – Bathsheba Jul 17 '18 at 07:51
  • I took the liberty of improving your wording as the downvotes and unhelpful comments you were getting were unfair. Hope you don't mind the edit. – acraig5075 Jul 17 '18 at 07:51
  • I took the liberty to change the mention of the evil that is gets() into references of fgets(). (@acraig5075: Two guys taking liberties at the same time. :-D ) – DevSolar Jul 17 '18 at 07:51

1 Answers1

5

I usually use std::getline().

For example, std::getline(std::cin, input); would read the first line from the console(std::cin means Standard Library::ConsoleInput) and store it in the variable input(which has to be of type std::string).

See equivalent of Console.ReadLine() in c++

Bathsheba
  • 231,907
  • 34
  • 361
  • 483