string class with pointers?

so im trying to write this code that will display
value of the pointer (not the address) of the parameter to the monitor.

i can get it to work with c-string but im having trouble with getting it to work with string
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  #include <iostream>
#include <string>

using namespace std;

void display(string* name);

int main()
{
	string name;
	string* name = new string();
	cout << "enter something: ";
	getline(cin,name);
	
	display(name);

  system("pause");
  return 0;
}

void display(string* name)
{
	string* namePtr = name;

	for( int i = 0; i < name.length(); i++)
	{
		cout << *namePtr ;
		namePtr++;
	}
	return;
}
check lines 10 and 11.
1
2
3
string name;
string *point; //you cannot use the same id, "name" for the pointer.
point=&name;  //let your pointer to string point to name 


and in your display function, you can just do: cout<<*name; and that's enough to display the value of name.

Aceix.
Thanks got it to work
Topic archived. No new replies allowed.