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
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
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."; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
