Question

#include <iostream> //write preprocessor file for string datatype here using namespace std; //write your function prototypes...

#include <iostream>
//write preprocessor file for string datatype here

using namespace std;

//write your function prototypes here:

int main()

{

    cout << "Welcome to Mad Lib.\n\n";

    cout << "Answer the following questions to help create a new story.\n";

    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)

{    
       //add your code here


}

int askNumber(string prompt)

{

          //add your code here

}

void tellStory(string name, string noun, int number, string bodyPart, string verb)

{

     cout << "\nHere's your story:\n";    
     cout << "The famous explorer ";    
     cout << name;

     cout << " had nearly given up a life-long quest to find\n";    
     cout << "The Lost City of ";    
     cout << noun;    
     cout << " when one day, the ";    
     cout << noun;    
     cout << " found the explorer.\n";    
     cout << "Surrounded by ";    
     cout << number;    
     cout << " " << noun;

     cout << ", a tear came to ";    
     cout << name << "'s ";    
     cout << bodyPart << ".\n";

     cout << "After all this time, the quest was finally over. ";    
     cout << "And then, the ";    
     cout << noun << "\n";    
     cout << "promptly devoured ";    
     cout << name << ". ";

     cout << "The moral of the story? Be careful what you ";    
     cout << verb;    
     cout << " for.";

}

Setting Up the Program

As usual, I start the program with some comments and include the necessary files.

// Mad-Lib

// Creates a story based on user input

#include <iostream>

//write preprocessor file for string datatype here

using namespace std;

//function prototype

//write your function prototypes here:

You can tell from my function prototypes that I have three functions in addition to main()—askText(), askNumber(), and tellStory().

The main() Function

The main() function calls all of the other functions. It calls the function askText() to get a name, plural noun, body part, and verb from the user. It calls askNumber() to get a number from the user. It calls tellStory() with all of the user-supplied information to generate and display the story.

int main()

{

cout << "Welcome to Mad Lib.\n\n";

cout << "Answer the following questions to help create a new story.\n";

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;

}

The askText() Function

The askText() function gets a string from the user. The function is versatile and takes a parameter of type string, which it uses to prompt the user. Because of this, I’m able to call this single function to ask the user for a variety of different pieces of information, including a name, plural noun, body part, and verb.

string askText(string prompt)

{

//add your code here

}

NOTE: Remember that this simple use of cin only works with strings that have no white space in them (such as tabs or spaces). So when a user is prompted for a body part, he can enter bellybutton, but medulla oblongata will cause a problem for the program.

There are ways to compensate for this, but that really requires a discussion of something called streams, which is beyond the scope of this Course. So use cin in this way, but just be aware of its limitations.

The askNumber() Function

The askNumber() function gets an integer from the user. Although I only call it once in the program, it’s versatile because it takes a parameter of type string that it uses to prompt the user.

int askNumber(string prompt)

{

            //add your code here

}

The tellStory() Function

The tellStory() function takes all of the information entered by the user and uses it to display a personalized story.

void tellStory(string name, string noun, int number, string bodyPart, string verb)

{

cout << "\nHere’s your story:\n"; cout << "The famous explorer "; cout << name;

cout << " had nearly given up a life-long quest to find\n"; cout << "The Lost City of "; cout << noun;

cout << " when one day, the "; cout << noun;

cout << " found the explorer.\n"; cout << "Surrounded by "; cout << number; cout << " " << noun;

cout << ", a tear came to "; cout << name << "’s "; cout << bodyPart << ".\n";

cout << "After all this time, the quest was finally over. "; cout << "And then, the "; cout << noun << "\n"; cout << "promptly devoured "; cout << name << ". ";

cout << "The moral of the story? Be careful what you "; cout << verb; cout << " for.";

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#include<iostream>
#include<string>
using namespace std;

string askText(string prompt){
string str;
cout<<prompt<<" :- ";
cin>>str;
//getline(cin, str);
return str;
}
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 << "\nHere's your story:\n";
cout << "The famous explorer ";
cout << name;
cout << " had nearly given up a life-long quest to find\n";
cout << "The Lost City of ";
cout << noun;
cout << " when one day, the ";
cout << noun;
cout << " found the explorer.\n";
cout << "Surrounded by ";
cout << number;
cout << " " << noun;
cout << ", a tear came to ";
cout << name << "'s ";
cout << bodyPart << ".\n";
cout << "After all this time, the quest was finally over. ";
cout << "And then, the ";
cout << noun << "\n";
cout << "promptly devoured ";
cout << name << ". ";
cout << "The moral of the story? Be careful what you ";
cout << verb;
cout << " for.";
}

int main(){

cout << "Welcome to Mad Lib.\n\n";
cout << "Answer the following questions to help create a new story.\n";

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;

}

output:

Add a comment
Know the answer?
Add Answer to:
#include <iostream> //write preprocessor file for string datatype here using namespace std; //write your function prototypes...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT