#include #include #include using namespace std; void OpenFile(ifstream & file); void GetBlank(string & result, ifstream & file); void ProcessLine(string line, string a, string b, string c, string d); string FillBlank(string tag, string a, string b, string c, string d); void Introduction(string lineA, string lineB, string lineC, string lineD); bool GetTag(string line, size_t & startPos, size_t & endPos, string & tag); int main() { ifstream inFile; string blank1, blank2, blank3, blank4; string line; OpenFile(inFile); GetBlank(blank1, inFile); GetBlank(blank2, inFile); GetBlank(blank3, inFile); GetBlank(blank4, inFile); if (inFile) { Introduction(blank1, blank2, blank3, blank4); } getline(inFile, line); while(inFile) { ProcessLine(line, blank1, blank2, blank3, blank4); getline(inFile, line); } inFile.close(); return 0; } void Introduction(string lineA, string lineB, string lineC, string lineD) { cout << endl; cout << "We will now tell a story about " << lineA << ", " << lineB << ", " << lineC << " and " << lineD << endl; cout << endl; return; } void OpenFile(ifstream & file) { string fileName; bool fileOpen = false; while (not fileOpen) { cout << "Enter the name of a madlib file => "; cin >> fileName; cin.ignore(100,'\n'); cout << endl; cout << "Attempting to open " << fileName << endl; file.open(fileName.c_str()); if (file) { cout << "Success " << endl; fileOpen = true; } else { cout << "Unable to open " << fileName << endl; } cout << endl << endl; } return; } void GetBlank(string & result, ifstream & file){ string description; char answer; bool goodAnswer = false; getline(file, description); if (!file) { cout << "Unable to read in a blank description" << endl; result = "NO TEXT ERROR"; } else { while (not goodAnswer) { cout << "Enter " << description << " => "; getline(cin, result); cout << "You said that " << result << " was " << description << endl; cout << "Is this ok? (Y,N) => "; cin >> answer; cin.ignore(100,'\n'); answer = toupper(answer); if (answer == 'Y') { goodAnswer = true; } } cout << endl; } return; } bool GetTag(string line, size_t & startPos, size_t & endPos, string & tag) { size_t pos; bool returnValue = true; startPos = line.find("<") ; if (startPos == string::npos) { returnValue = false; } endPos = line.find(">", startPos); tag = line.substr(startPos+1, endPos-startPos-1); return returnValue; } void ProcessLine(string line, string a, string b, string c, string d){ size_t startPos, endPos; string outputLine; string tag; while (GetTag(line, startPos, endPos, tag)) { outputLine = outputLine + line.substr(0, startPos); line = line.substr(endPos+1, line.size()); outputLine = outputLine + FillBlank(tag, a,b,c,d); } outputLine += line; cout << outputLine << endl; return; } string FillBlank(string tag, string a, string b, string c, string d) { string giveBack; if(tag == "1") { giveBack = a; } else if (tag =="2") { giveBack = b; } else if (tag == "3") { giveBack = c; } else if (tag == "4") { giveBack = d; } else { giveBack = "Bad Tag"; } return giveBack; }