Take the code and change it to use references to be more efficient and safe. (If
ID: 3809992 • Letter: T
Question
Take the code and 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)
// Mad-Lib
// 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 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
Hi
I have updated the code and highighted the code changes below,
#include <iostream>
#include <string>
using namespace std;
void askText(string prompt, string &text);
void askNumber(string prompt, int &number);
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: ", name);
string noun;
askText("Please enter a plural noun: ", noun);
int number;
askNumber("Please enter a number: ", number);
string bodyPart;
askText("Please enter a body part: ", bodyPart);
string verb;
askText("Please enter a verb: ", verb);
tellStory(name, noun, number, bodyPart, verb);
return 0;
}
void askText(string prompt, string &text)
{
cout << prompt;
cin >> text;
}
void askNumber(string prompt,int &num)
{
cout << prompt;
cin >> 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.";
}
Output:
sh-4.2$ g++ -o main *.cpp
sh-4.2$ main
Welcome to Mad Lib.
Answer the following questions to help create a new story.
Please enter a name: Suresh
Please enter a plural noun: s
Please enter a number: 111
Please enter a body part: hand
Please enter a verb: dd
Here's your story:
The famous explorer Suresh had nearly given up a life-long quest to find
The Lost City of s when one day, the s found the explorer.
Surrounded by 111 s, a tear came to Suresh's hand.
After all this time, the quest was finally over. And then, the s
promptly devoured Suresh. The moral of the story? Be careful what you dd for.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.