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

MAD LIBS C++ Your program will read the contents of a text file that contains th

ID: 3568525 • Letter: M

Question

MAD LIBS C++

Your program will read the contents of a text file that contains the appropriate needed parts of speech as well as the story. You should prompt the user to enter in the name of a text file (and your program should continue to ask the person to enter a text file name until they enter a valid filename. Your MadLibs implementation will allow the user to play multiple times until the user indicates that they are ready to quit. You should display your instructions to the user at the *START* of the game.

example txt file:

You are required to write the following three functions (you will have additional functions):

Explanation / Answer

#include<iostream>
#include<fstream>
#include<string>
#include <stdlib.h>
using namespace std;

void readGameFile(string *entries, int *num_entries, string *story, int *num_lines) {
string line;
  
char inputFile[20];
cout<<"Enter the input file: ";
cin>>inputFile;
cout<<" ";
  
ifstream myfile(inputFile);
if (myfile.is_open()) {
  
getline(myfile, line);
*num_entries = atoi(line.c_str());
  
for(int i=0; i<*num_entries; i++)
{
getline(myfile, entries[i]);
}
  
getline(myfile, line);
*num_lines = atoi(line.c_str());
  
for(int i=0; i<*num_lines; i++)
{
getline(myfile, story[i]);
}
myfile.close();
}
else cout << " Unable to open file !! ";
}

void getUserInput(string *entries, int *num_entries, string *user_input)
{
for(int i=0; i<*num_entries; i++)
{
cout<<"Enter "<<entries[i]<<" > ";
cin>>user_input[i];
  
cout<<" ";
}
}

void createStory(string *user_input, string *story, int *num_lines)
{
int k = 0;
for(int i=0; i<*num_lines; i++)
{
while(true)
{
size_t found = story[i].find("*");
if (found!=std::string::npos)
{
story[i].replace(found, 1, user_input[k++]);
}
else
{
break;
}
}
  
  
}
}
void print(string *story, int *num_lines)
{
for(int i=0; i<*num_lines; i++)
{
cout<<story[i]<<" ";
}
}
int main() {

string entries[10];
int num_entries;
string story[100];
int num_lines;
  
string user_input[10];

readGameFile(entries, &num_entries, story, &num_lines);
  
getUserInput(entries, &num_entries, user_input);
  
createStory(user_input, story, &num_lines);
  
cout<<" The MAD LIBS game results to: ";
  
print(story, &num_lines);


return 0;
}

------------------------------------------

input.txt

4
adjective
famous person
number
type of food
3
The * computer science professor reminds me
of *. I bet she has * dachshunds and eats
* every day.

------------------------------------------------------------------------------------

OUTPUT