Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 2

#include <iostream>

#include <fstream>
#include <cctype>
#include <cstring>
using namespace std;

/***********************************************************************
* getFilename() This function gets the file name from the user and passes
* it to the other functions.
***********************************************************************/
void getFilename(char fileName[])
{
cout << "Please enter the filename of the Mad Lib: ";
cin >> fileName;
}

/***********************************************************************
* askQuestions() This function turns the prompts in the file into questions
* and asks the user for the information.
***********************************************************************/
void askQuestions(char story[])
{
if (story[0] != '<' || !isalpha(story[1]))
return;
else if (story[0] == '<' && isalpha(story[1]))
{
cout << "\t";
story[1] = toupper(story[1]);
cout << story[1];
for (int iStory = 1; story[iStory] == '>'; iStory++)
{
if (story[iStory] == '_')
cout << " ";
else
{
story[iStory] = tolower(story[iStory]);
cout << story[iStory];
}
}
cout << ": ";
cin >> story;
}
cout << "\nThank you for playing.\n";
}

/***********************************************************************
* readFile() This function reads the Mad Lib file and passes information
* to the askQuestions function.
***********************************************************************/
int readFile(char story[][33], char fileName[])
{
ifstream fin(fileName);
if (fin.fail())
{
cout << "Error reading file " << fileName << ".\n";
return 0;
}
int numWords = 0;
while (fin >> story[numWords])
{
askQuestions(story[numWords]);
numWords++;
}
if (numWords > 256)
{
cout << "Error reading file " << fileName
<< ". It has more than 256 words.";
return 0;
}
fin.close();
return numWords;
}

/**********************************************************************
* main() The main function calls the getFilename and readFile functions.
***********************************************************************/
int main()
{
char story[256][33];
char fileName[256];
getFilename(fileName);
int wordCount = readFile(story, fileName);
return 0;
}

You might also like