hw8fncts.cpp

/*

Programmer: Jennifer Leopold

Date: March 14, 2016

File: hw8_functs.cpp

Purpose: This file contains the definitions of the functions

for the program that generates poetry.

*/

#include <iostream>

#include <fstream>

#include <cstdlib>

#include <ctime>

#include <string.h>

#include <stdlib.h>

#include <cstring>

#include "hw8_functs.h"

using namespace std;

int getIntInput(const string prompt, const int lowerBound,

const int upperBound)

{

int x;

bool invalidEntry;

do

{

cout << prompt;

cin >> x;

invalidEntry = (x < lowerBound) || (x > upperBound);

if (invalidEntry)

cout << "Please enter an integer between "

<< lowerBound << " and " << upperBound << ".\n";

} while (invalidEntry);

return(x);

}

void generatePoem(const short numLines)

{

string poemPattern[MAX_WORDS_IN_POEM_LINE];

short numWordsInPattern;

string poemWord;

getPoemPattern(poemPattern, numWordsInPattern);

// For each line, and each word type in pattern, randomly

// generate an appropriate word

for (int line = 1; line <= numLines; line++)

{

for (int wordNum = 0; wordNum < numWordsInPattern;

wordNum++)

{

// Convert this entry in the poemPattern to a

// cstring so that we can use atoi on it and

// determine whether it is an integer

char cstring[MAX_CHARS_IN_POEM_WORD];

strcpy(cstring, poemPattern[wordNum].c_str());

int wordType = atoi(cstring);

// If this pattern entry represents an int word type,

// randomly choose that type of word from the

// appropriate file and output it; otherwise,

// just output the entry literally

if ((wordType > 0) && (wordType <= NUM_WORD_TYPES))

poemWord = getRandomWordFromFile(wordType);

else poemWord = poemPattern[wordNum];

cout << poemWord << " ";

}

cout << endl;

}

return;

}

string getRandomWordFromFile(const short wordType)

{

ifstream fin;

char filename[MAX_LENGTH_FILENAME];

string fileNameAsString;

string chosenWord;

int numEntriesInFile, wordNum, chosenWordNum;

fileNameAsString = WORD_FILENAMES[wordType-1];

strcpy(filename, fileNameAsString.c_str());

strcat(filename, ".dat");

fin.clear(); // reset connection

fin.open(filename);

if (!fin)

cout << "Cannot open " << filename << "!\n";

else

{

fin >> numEntriesInFile;

chosenWordNum = (rand() % numEntriesInFile) + 1;

// Read lines in this word file until you reach

// the chosen line #

wordNum = 0;

while (wordNum != chosenWordNum)

{

fin >> chosenWord;

wordNum++;

}

fin.close();

}

return(chosenWord);

}

void getPoemPattern(string poemPattern[],

short &numWordsInPattern)

{

ifstream fin;

string wordType;

int numEntriesInFile;

int patternNum;

fin.clear(); // reset connection

char filename[MAX_LENGTH_FILENAME];

strcpy(filename, PATTERN_FILENAME.c_str());

fin.open(filename);

if (!fin)

cout << "Cannot open " << PATTERN_FILENAME << "!\n";

else

{

fin >> numEntriesInFile;

// Choose random number corresponding to a pattern

// in the file

patternNum = (rand() % numEntriesInFile) + 1;

// Read over patterns 1..patternNum-1 in the file

for (short i = 1; i < patternNum; i++)

{

do

{

fin >> wordType;

} while (wordType != END_OF_PATTERN);

}

// Collect the wordTypes and literal words for the

// pattern patternNum into poemPattern[]

numWordsInPattern = 0;

fin >> wordType;

while (wordType != END_OF_PATTERN)

{

poemPattern[numWordsInPattern] = wordType;

numWordsInPattern++;

fin >> wordType;

}

fin.close();

}

return;

}