#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.";
}
#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:
#include <iostream> //write preprocessor file for string datatype here using namespace std; //write your function prototypes...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include <iostream> #include <cstring> #include <string> #include <istream> using namespace std; //Function prototypes int numVowels(char *str); int numConsonants(char *str); int main() { char string[100]; char inputChoice, choice[2]; int vowelTotal, consonantTotal; //Input a string cout << "Enter a string: " << endl; cin.getline(string, 100); do { //Displays the Menu cout << " (A) Count the number of vowels in the string"<<endl; cout << " (B) Count...
#include#includeusing namespace std;// Implement printArray here// Implement deleteSmallest here// DO NOT CHANGE MAIN FUNCTION BELOWint main() {int myarray[100];cout << "Enter number of integers : ";int n;cin >> n;cout << "Enter " << n << " integers" << endl;for (int i = 0; i < n; i++)cin >> myarray[i];cout << "Contents of array : ";printArray(myarray, n);deleteSmallest(myarray, n);cout << "Contents of array after deleteSmallest: ";printArray(myarray, n-1);}
please help me fix the error in here #include<iostream> #include <string> using namespace std; string getStudentName(); double getNumberExams(); double getScoresAndCalculateTotal(double E); double calculateAverage(double n, double t); char determineLetterGrade(); void displayAverageGrade(); int main() { string StudentName; double NumberExam, Average, ScoresAndCalculateTotal; char LetterGrade; StudentName = getStudentName(); NumberExam = getNumberExams(); ScoresAndCalculateTotal= getScoresAndCalculateTotal(NumberExam); Average = calculateAverage(NumberExam, ScoresAndCalculateTotal); return 0; } string getStudentName() { string StudentName; cout << "\n\nEnter Student Name:"; getline(cin, StudentName); return StudentName; } double getNumberExams() { double NumberExam; cout << "\n\n Enter...
#include <iostream> #include <string> using namespace std; void get_user_string(string *); string convert_to_dash(String* ); int_search_and_replace(char, string, string &); int main (){ string s; cout << "Enter a string:" << endl; get_user_string(&s); string dash_version = convert_to_dash(&s) if ( dash_version != 32){ &s.push_back('-'); } Here is an example operation of the completed program: Please enter a string: the weather is great! The dash-version of your string is: Please tell me the char that...
#include <iostream> #include <cstdlib> #include <time.h> #include <string> using namespace std; int main() { srand(time (0)); int number, guess, response, reply; int score = 0 number = rand() % 100 + 1; do { do { cout << "Enter your guess "; cin >> guess; score++; if (guess < number) cout << guess << " is too low! Enter a higher number. "; else if (guess > number) cout << guess << " is too high! Enter a lower number....
c++ #include <iostream> #include <string> using namespace std; /* Write a function checkPrime such that input: an int output: boolean function: Return true if the number is a prime number and return false otherwise */ //TO DO ************************************** /* Write a function checkPrime such that input: an array of int and size of array output: nothing function: Display the prime numbers of the array */ //TO DO ************************************** /* Write a function SumofPrimes such that input: an int output: nothing...
#include <iostream> #include <fstream> using namespace std; //constants const int CAP = 100; //function prototypes bool openFile(ifstream &); void readData(ifstream &, int [], int &); void printData(const int [], int); void sum(const int[], int); void removeItem(int[], int &, int); int main() { ifstream inFile; int list[CAP], size = 0; if (!openFile(inFile)) { cout << "Program terminating!! File not found!" << endl; return -1; } //read the data from the file readData(inFile, list, size); inFile.close(); cout << "Data in file:" <<...
#include <fstream> #include <iostream> #include <cstdlib> using namespace std; // Place charcnt prototype (declaration) here int charcnt(string filename, char ch); int main() { string filename; char ch; int chant = 0; cout << "Enter the name of the input file: "; cin >> filename; cout << endl; cout << "Enter a character: "; cin.ignore(); // ignores newline left in stream after previous input statement cin.get(ch); cout << endl; chcnt = charcnt(filename, ch); cout << "# of " «< ch« "'S:...
#include <iostream> #include <string> using namespace std; //Write a function that changes all characters in a string to dashes string to_dash(string s){ for(int i = 0; i < s.length(); i++){ } return s; } int main(){ string s; cin >> s; s = to_dash(s); cout << s << endl; }