Why is it not showing the values

When i dereference the pointers, there is no value showing but when i don't dereference the pointers it shows the addresses. I don't understand why it wont show the values i am putting in. Please help.

//Mad-Lib-Pointers

#include <iostream>
#include <string>

using namespace std;

string ask_text(string prompt);
int ask_number(string prompt);
void tell_story(string* const pName, string* const pNoun, int number,
string* const pBody_part, string* const pVerb);

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

string* pName = &ask_text("Please enter a name: ");
string* pNoun = &ask_text("Please enter a plural noun: ");
int number = ask_number("Please enter a number: ");
string* pBody_part = &ask_text("Please enter a bodypart: ");
string* pVerb = &ask_text("Please enter a verb: ");

tell_story(pName, pNoun, number, pBody_part, pVerb);

return 0;
}

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

string ask_text(string prompt)
{
string text;
cout<<prompt;
cin>>text;
return text;
}


void tell_story(string* const pName, string* const pNoun, int number,
string* const pBody_part, 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<<number;
cout<<" "<<*pNoun;
cout<<", a tear came down to ";
cout<<*pName<<"'s ";
cout<<*pBody_part<<".\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? Becareful what you ";
cout<<*pVerb;
cout<<" for.";
}
Your program is invalid and I do not understand why you are using pointers to string instead of strings themself. For example in this statement

string* pName = &ask_text("Please enter a name: ");

you are assigning pName the address of a temporary object returned by the function that will be deleted at once after executing the statement. So the program behavior is undefined.
Last edited on
Topic archived. No new replies allowed.