+ 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.
14th Jan 2018, 10:12 PM
Jacob Pembleton
Jacob Pembleton - avatar
0
may I ask why are you declaring a pointer in middle of a variable name ?
14th Jan 2018, 9:05 PM
Cain Eviatar
Cain Eviatar - avatar