New course! Every coder should learn Generative AI!
Try a free lesson+ 3
C++ stuff
In my code, I was creating some document writer, however there was a problem which hindered my progress. I was wondering if anyone could correct it. Many thanks in advance. https://code.sololearn.com/cb7nEi3ZE703/#cpp
3 Answers
+ 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 ?
0
im new to c++. So, I don't know a lot and a lot of stuff I haven't become accustomed with. I am used to the layout of js or html