-4

As evident from question itself, I'm new to C++ and also pointers and dynamic memory allocation.

I want to know how do I stop using static strings like

char str[200]; 

and should be able to take input until user press enter?

I know this could be done using "new" and I would free memory using "delete" but I can't think of a way to implement this.

Please be polite :) and I know about std::string, but just want to know how can I implement above mention problem on my own.

Elazar
  • 20,415
  • 4
  • 46
  • 67
Keshav Sharma
  • 182
  • 2
  • 13
  • 1
    http://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list – user2296177 May 21 '16 at 21:47
  • The one of the most useful skills in programming is to know how to **search**. http://www.cplusplus.com/doc/tutorial/pointers/ http://www.cplusplus.com/doc/tutorial/dynamic/ – PcAF May 21 '16 at 21:57
  • 1
    For anyone that cares and is trying to parse this question: I tried reading this multiple times, and in the end the only reasonable quest I could recognize was fed by the phrase: *"... I know this could be done using "new" and I would free memory using "delete" but I can't think of a way to implement this."*, which I believe to be a request to be shown how to use `new/delete` to manage a string buffer. – WhozCraig May 21 '16 at 22:02
  • _@Keshav_ Well, if you'll need to know how to use `new` and `delete` balanced correctly have a look at [What is The Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three). – πάντα ῥεῖ May 21 '16 at 22:31

1 Answers1

3

C++ beginners don't need to use pointers, new/delete, nor arrays. In fact, many experts will avoid using them also. Just keep things simple

std::string name;
std::cout << "What is your name?" << std::endl;
std::getline(std::cin, name); // read a line from std::cin (standard input)
std::cout << "Hello, " << name << std::endl;
πάντα ῥεῖ
  • 1
  • 13
  • 116
  • 190
Aaron McDaid
  • 26,501
  • 9
  • 66
  • 88
  • 1
    Last paragraph of question: *i know about std::string, but just want to know how can i implement above mention problem on my own*. – PcAF May 21 '16 at 21:58
  • I saw that. Perhaps I may have misunderstood the context of the question. Either, the questioner simply wants to use strings in a simple way while learning the basics of C++; or else the questioner is simply curious about how `string` is implemented. – Aaron McDaid May 21 '16 at 22:08
  • @KeshavSharma, "please give me implementation". We don't know what your real question is. You may be surprised by this, but it's important that you accept that we are unsure about what you are asking. – Aaron McDaid May 22 '16 at 14:09
  • And @KeshavSharma, you should care about downvotes on the question, as it means that others won't see your question. If you want a good answer, then ask a good (novel) question! – Aaron McDaid May 22 '16 at 14:12