Need advice on pointers

Hey, so this book I'm following, this is the previous used code that basically just asks for some user input and then creates a custom story with that information.

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
58
59
60
61
62
63
64
65
66
67
68
// Mad lib game - Creates a story based on user input

#include <iostream>
#include <string>

using namespace std;

string askText(string prompt);
int askNumber(string prompt);
void tellStory(string name, string noun, int number, string bodyPart, string verb);

int main() {
	cout << "Welcome to the Mad Lib.\n\n";
	cout << "Answer the following questions to help create a new story.\n\n";

	string name = askText("Please enter a name: ");
	string noun = askText("Please enter a plural noun: ");
	int number = askNumber("Please enter a number: ");
	string bodyPart = askText("Please enter a body part: ");
	string verb = askText("Please enter a verb: ");

	tellStory(name, noun, number, bodyPart, verb);

	return 0;

}

string askText(string prompt) {

	string text;
	cout << prompt;
	cin >> text;
	return text;
}

int askNumber(string prompt) {
	int num;
	cout << prompt;
	cin >> num;
	return num;
}

void tellStory(string name, string noun, int number, string bodyPart, string verb) {
	cout << "\nHere's your story: \n";
	cout << "The famous explorer ";
	cout << name;
	cout << " had nearly given up a life-long quest to find\n";
	cout << "The Lost City of ";
	cout << noun;
	cout << " when one day, the ";
	cout << noun;
	cout << " found the explorer.\n";
	cout << "Surrounded by ";
	cout << number;
	cout << " " << noun;
	cout << ", a tear came to ";
	cout << name << "'s ";
	cout << bodyPart << ".\n";
	cout << "After all this time, the quest was finally over.";
	cout << "\nAnd then, the ";
	cout << noun << "\n";
	cout << "promptly devoured ";
	cout << name << ", ";
	cout << "The moral of the story? Be careful what you ";
	cout << verb;
	cout << " for.\n";

}


The task is to "Rewrite the mad lib game so that no string objects are passed to the function that tells the story. Instead, the function should accept pointers to string objects".

This is what I've come up with from what I know; it functions correctly but is it right?

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
58
59
60
61
62
63
64
65
66
67
68
  //Creates a story based on user input using pointers

#include <iostream>
#include <string>

using namespace std;

string askText(string prompt);
int askNumber(string prompt);
void tellStory(string* name, string* noun, int number, string* bodyPart, string* verb);

int main() {
	cout << "Welcome to the Mad Lib.\n\n";
	cout << "Answer the following questions to help create a new story.\n\n";

	string name = askText("Please enter a name: ");
	string noun = askText("Please enter a plural noun: ");
	int number = askNumber("Please enter a number: ");
	string bodyPart = askText("Please enter a body part: ");
	string verb = askText("Please enter a verb: ");

	tellStory(&name, &noun, number, &bodyPart, &verb);

	return 0;

}

string askText(string prompt) {

	string text;
	cout << prompt;
	cin >> text;
	return text;
}

int askNumber(string prompt) {
	int num;
	cout << prompt;
	cin >> num;
	return num;
}

void tellStory(string* name, string* noun, int number, string* bodyPart, string* verb) {
	cout << "\nHere's your story: \n";
	cout << "The famous explorer ";
	cout << *name;
	cout << " had nearly given up a life-long quest to find\n";
	cout << "The Lost City of ";
	cout << *noun;
	cout << " when one day, the ";
	cout << *noun;
	cout << " found the explorer.\n";
	cout << "Surrounded by ";
	cout << number;
	cout << " " << *noun;
	cout << ", a tear came to ";
	cout << *name << "'s ";
	cout << *bodyPart << ".\n";
	cout << "After all this time, the quest was finally over.";
	cout << "\nAnd then, the ";
	cout << *noun << "\n";
	cout << "promptly devoured ";
	cout << *name << ", ";
	cout << "The moral of the story? Be careful what you ";
	cout << *verb;
	cout << " for.\n";

}


Thanks for any advice.
Last edited on
Yes, it takes string pointers, it process them, it does not give any errors, it is working correctly, so it is correct.
Thanks, that's good to know.
Topic archived. No new replies allowed.