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

getFilename(fileName) OPEN fin from fileName IF error in fin DISPLAY error RETUR

ID: 3727297 • Letter: G

Question

getFilename(fileName)

OPEN fin from fileName

IF error in fin

DISPLAY error

RETURN 0

WHILE numWords < 256 and READ story[numWords] from fin

askQuestions(story[numWords])

numWords++

CLOSE fin

RETURN numWords

END

Pseudocode for askQuestions()

askQuestions(text)

IF text[0] < or !isalpha(text[1])

return

PUT tab and toupper(text[1])

FOR iText = 2 until text[iText] = >

IF text[iText] = _

PUT space

ELSE

PUT tolower(text[iText])

PUT colon and space

GET text

END

Using the structure chart above and the Pseudocode

with c++

write the code necessary read the Mad Lib from a file and prompt the user:

Note that there is a tab before each of the questions (ex: "Plural noun:")

Hints

Your program will not need to be able to handle files of unlimited length. The file should have the following properties (though you will need to do error-checking to make sure):

There are no more than 1024 characters total in the input file.

There are no more than 32 lines in the input file.

Each line in the input file has no more than 80 characters in it.

There are no more than 256 words in the input file including prompts.

Each word in the input file is no more than 32 characters in length.

Hint: To see how to declare and pass an array of strings, please see Chapter 3.0 of the text.

Assignment

Perhaps the easiest way to do this is in a four-step process:

Create the framework for the program using stub functions based on the structure chart from your design document.

Write each function. Test them individually before "hooking them up" to the rest of the program. You are not allowed to use the String Class for this problem; only c-strings!

Structure chart: main stor story, le continue ngth readFile playAgain displayStory fileName character isPunc getFilename askQuestions isPunc

Explanation / Answer

#include <iostream>
#include <fstream>
#include <locale>
#include <ctype.h>
using namespace std;

//Asks for a filename
void getFile(char fileName[])
{
cout << "Please enter the filename of the Mad Lib: ";
cin >> fileName;
cin.ignore();
return;
}


//reads file into a multi array
void readFile(char fileName[], char file[][32], int &wordCount)
{


ifstream fin;
fin.open(fileName);

if(fin.fail())
{
return;
}

while(fin >> file[wordCount])
{
wordCount++;
}

fin.close();
return;

}


// Function: promptUser


void promptUser(char file[][32], int wordCount, char promptsAnswers[],
int& promptCount)
{
//create a prompt arrary to save the prompts to
char prompt[256][32];

/*search for the signal while saving the prompt count then putting the info
into the prompt array */
for(int i = 0; i < wordCount; i++)
{
if (file[i][0] == '<')
{
for(int k = 0; file[i][k]; k++)
{
prompt[promptCount][k] = file[i][k];
}
promptCount++;
}
}


/* switches making the prompt correct */
for(int v = 0, f = 0; v < promptCount; f++)
{
  
if (prompt[v][f] == '<')
prompt[v][f] = ' ';

else if (prompt[v][f] == '>')
{
prompt[v][f] = ':';
prompt[v][f+1] = ' ';
v++;
f = -1;
}

else if (prompt[v][f] == '_')
prompt[v][f] = ' ';
}

for(int q = 0; q < promptCount; q++)
{
if(islower(prompt[q][1]))
prompt[q][1] = toupper(prompt[q][1]);

if(prompt[q][1] != '#' && prompt[q][1] != '{' && prompt[q][1] != '}'
&& prompt[q][1] != '[' && prompt[q][1] != ']')
{
cout << prompt[q];
cin.getline(promptsAnswers, 256);
}
}
  

return;
}

void display(char file[][32], int wordCount, char promptsAnswers[],
int promptCount)
{

}

int main()
{
char promptsAnswers[256];
int wordCount = 0;
int promptCount = 0;
char fileName[256];
char file[256][32];
getFile(fileName);
readFile(fileName, file, wordCount);
promptUser(file, wordCount, promptsAnswers, promptCount);
display(file, wordCount, promptsAnswers, promptCount);
cout << "Thank you for playing. ";
}