Please help with Mad Lib Pointer code problem

The issue is I need to use pointers to pass the variables to the function that tells the story. I've tried this every which way but obviously there is a way i have not or i wouldn't be posting asking for assistance.

If anyone could help that would be great!

Here's the code i have so far.

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
69
70
71
72
73
74
75
76
// Mad-Lib
// Creates a story based on user input
//instructions: Rewrite the final project from Chapter 5, 
//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.


#include <iostream>
#include <string>

using namespace std;

string askText(string prompt);
int askNumber(string prompt);
void ptellStory(string* const pname, string* const pnoun, int* const pnumber, string* const pbodyPart, string* const pverb);

int main()
{
    cout << "Welcome to Mad Lib.\n\n";
    cout << "Answer the following questions to help create a new story.\n";
    
    const string name = askText("Please enter a name: ");
    const string noun = askText("Please enter a plural noun: ");
    int number = askNumber("Please enter a number: ");
    const string bodyPart = askText("Please enter a body part: ");
    const string verb = askText("Please enter a verb: ");
    
ptellStory(*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 ptellStory(string const pname, string const pnoun, int const pnumber, string const pbodyPart, string const pverb);
{
    cout << "\nHere's your story:\n";
    cout << "The famous explorer ";
    cout << &pname;
    cout << " had nearly given up a life-long quest to find\n";
    cout << "The Lost City of ";
    cout << &pnoun;
    cout << " when one day, the ";
    cout << &pnoun;
    cout << " found the explorer.\n";
    cout << "Surrounded by ";
    cout << &pnumber;
    cout << " " << &pnoun;
    cout << ", a tear came to ";
    cout << &pname << "'s ";
    cout << &pbodyPart << ".\n";
    cout << "After all this time, the quest was finally over. ";
    cout << "And then, the ";
    cout << &pnoun << "\n";
    cout << "promptly devoured ";
    cout << &pname << ". ";
    cout << "The moral of the story? Be careful what you ";
    cout << &pverb;
    cout << " for.";
}
Could really use some direction on this....pleeeease :)
closed account (D80DSL3A)
A couple of problems I see.
1) Make the function definition match the prototype. Line 50 should be:
void ptellStory(string* const pname, string* const pnoun, int* const pnumber, string* const pbodyPart, string* const pverb)// no ; here!

2) You have the roles of * and & reversed in your code.
Line 29 should be: ptellStory(&name, &noun, &number, &bodyPart, &verb);.
In the function ptellStory() everywhere you are using a & you should be using a * instead.
eg. line 54 should be cout << *pname; instead of cout << &pname;.
@fun2code

I found your way didn't work either. The problem was still line 50. I had to change it, and the declaration, to void ptellStory(string const *pname, string const *pnoun, int const *pnumber, string const *pbodyPart, string const *pverb) with the pointer with the names, not after the types. Explanation #2, correct.
Thank you Thank you Thank you!!
once i made the changes you pointed out and removed the ; from the function line 50, all compiled and went as planned
Last edited on
Topic archived. No new replies allowed.