Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

1. Take the code change it to use references to be more efficient and safe. (If

ID: 3709850 • Letter: 1

Question

1. Take the code change it to use references to be more efficient and safe. (If you don't expect the variable to change in the function, do not allow it to change)

#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 Mad Lib. ";
cout << "Answer the following questions to help create a new story. ";
  
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 << " Here's your story: ";
cout << "The famous explorer ";
cout << name;
cout << " had nearly given up a life-long quest to find ";
cout << "The Lost City of ";
cout << noun;
cout << " when one day, the ";
cout << noun;
cout << " found the explorer. ";
cout << "Surrounded by ";
cout << number;
cout << " " << noun;
cout << ", a tear came to ";
cout << name << "'s ";
cout << bodyPart << ". ";
cout << "After all this time, the quest was finally over. ";
cout << "And then, the ";
cout << noun << " ";
cout << "promptly devoured ";
cout << name << ". ";
cout << "The moral of the story? Be careful what you ";
cout << verb;
cout << " for.";
}

Explanation / Answer

#include <iostream>
#include <string>

using namespace std;

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

int main()
{
    cout << "Welcome to Mad Lib. ";
    cout << "Answer the following questions to help create a new story. ";
  
    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(const string &prompt)
{
    string text;
    cout << prompt;
    cin >> text;
    return text;
}

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

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