+ 3
#include <iostream>
#include <string>
using namespace std;
int main() {
string *doc = new string;
cin >> *doc;
cout << *doc << endl;
delete doc;
return 0;
}
In the lesson examples, they use p as the variable name to stand for pointer. It is not a keyword so you can just make the pointer named doc like this:
string *doc = new string;
Also, cin doesnât return a value. To get input from it and store that input in the pointer doc:
cin >> *doc;
No = is needed.
Finally, you should use
delete doc;
to delete the memory location that doc is pointing to. Itâs not important if the program is about to exit, but if you use a lot of pointers and never delete when you are done with them, they will build up and slow the program down a lot.
0
may I ask why are you declaring a pointer in middle of a variable name ?