Advice??/ question

this is just a small program i wrote to practice using pointers, and passing them into functions, and also showing that you can modify the values in an address using a pointer by purposely getting the last name wrong. I just want to know if im doing this properly and im actually showing that the value is in fact being modified.

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
  #include <iostream>
#include <string>

using namespace std;

void getInfo(string, string);

void printGreeting(string*, string*);

int main()
{
	string first, last;
	
	
	getInfo(first, last);
	






	cout << "\n\n";
	system("PAUSE");
	return 0;

}

void getInfo(string newFirst, string newLast)
{
	

	cout << "What is your first name? ";
	getline(cin, newFirst);
	cout << "\n\n";

	cout << "What is your last name? ";
	getline(cin, newLast);
	cout << "\n\n";

	string *p_first = &newFirst;
	string *p_last = &newLast;

	printGreeting(p_first, p_last);
}

void printGreeting(string *p_newFirst, string *p_newLast)
{
	cout << "Hello " << *p_newFirst << " " << *p_newLast;

	*p_newLast = "Caughing";

	cout << "\n\nOr is it " << *p_newFirst << " " << *p_newLast;

}
The real test is after your function returns (in main in your case.) You should print the values after the calls to your functions.

Also, the best way to pass parameters is to use references.
so your saying that rather than call printGreeting(p_first, p_last); in void getInfo(string newFirst, string newLast)... i should be calling it in main()?????

also i understand that passing parameters using reference is the better idea, but the whole purpose of this is that i am learning pointers and how to properly use them so this is strictly educational as i am a beginner.
I don't think the strings you are passing to getinfo() are doing anything. They are empty strings and are being passed by value ( a copy). newfirst is a local variable and is getting assigned the empty string you passed by value. It does nothing with this empty string and then gets assigned the value from cin.

You should do the prototypes either getinfo(string& first, string& last) or getinfo(string *first, string *last)

Then call the function in nmain with getinfo(first,last); (in the reference version- first case)

or

getinfo(&first,&last); (if you use the pointer version- second case)

That way the strings you are passing from main will be changing and you can test this with cout statement in main.
Last edited on
ok, how about this??? (some changes)

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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>

using namespace std;

void getInfo(string &first, string &last);

void printGreeting(string*, string*);

int main()
{
	string first, last;
	string *p_first = &first;
	string *p_last = &last;
	
	getInfo(first, last);
	printGreeting(p_first, p_last);






	cout << "\n\n";
	system("PAUSE");
	return 0;

}

void getInfo(string &newFirst, string &newLast)
{
	

	cout << "What is your first name? ";
	getline(cin, newFirst);
	cout << "\n\n";

	cout << "What is your last name? ";
	getline(cin, newLast);
	cout << "\n\n";

	

	
}

void printGreeting(string *p_newFirst, string *p_newLast)
{
	

	cout << "Hello " << *p_newFirst << " " << *p_newLast;

	*p_newLast = "Caughing";

	cout << "\n\nOr is it " << *p_newFirst << " " << *p_newLast;

}


also i would like to know if i am truly modifing *p_last in this line of code?
*p_newLast = "Caughing";
@MasartiDeluxe: Sorry: I misread that you were calling that function to see the results of your getInfo() function. Forget what I said about testing from main -- You already had a function to test the result of getInfo().

Do you get any warnings in print Greeting() in your new version?
i dont it actually compiles just as i wanted it too.
Yes, now you are changing the strings you declared in main with both functions. You pass both strings by reference to getInfo() and you assign them the input from cin. To return to main and you pass pointers to these strings to printGreeting(). In prinGreeting you change the string you declared in main when you assign "Caughing" to the string that p_newLast is pointing to.

Add this to to main right before system("PAUSE")

1
2
3
cout << last <<'\n';
 cout << *p_last << '\n';




Last edited on
thanks! i did add your code and it does modify the value in string last; which is exactly what i wanted to see, i didnt think to do that..... but it was good for me to see that change so i can better understand pointers thanks again!
Anytime!
Topic archived. No new replies allowed.