#define ALLOW_INSTRUCTOR_CODE /*************************************************************************** *************************************************************************** ****** BEFORE BEGINNING OR TURNING IN THIS EXERCISE IT IS EXTREMELY ****** ****** IMPORTANT THAT YOU READ, FULLY UNDERSTAND, AND ADHERE TO THE ****** ****** REQUIREMENTS STATED IN THE DOCUMENT TITLED, "ASSIGNMENT ****** ****** SUBMISSION REQUIREMENTS", DOWNLOADABLE FROM THE COURSE WEB ****** ****** SITE. ****** *************************************************************************** **************************************************************************/ /*************************************************************************** ************* C/C++ Programming 2: Assignment 2 - Exercise 4 ************* ******** BEGIN EXERCISE REQUIREMENT - THIS MUST BE A "C++" PROGRAM ******** *************************************************************************** (3 points) Write functions OpenFile and Reverse: OpenFile syntax: void OpenFile(const char *fileName, ifstream &inFile); Parameters: fileName - a pointer to the name of a file to be opened inFile - a reference to the ifstream object to be used to open the file Synopsis: Opens the file named in in the read-only text mode using the object. If the open fails an error message is output to cerr and the program is terminated with an error exit code. Return: void if the open succeeds; otherwise, the function does not return. Reverse syntax: int Reverse(ifstream &inFile, int level); Parameters: inFile - a reference to an ifstream object representing a text file open in a readable text mode. level - recursive level of this function call: 1 => 1st call, 2 => 2nd call, etc. Synopsis: Recursively reads one character at a time from the text file in until a separator is encountered. Those characters are then displayed in reverse order, with the last character displayed being capitalized. Finally, the separator is returned to the calling function. Separators are not reversed. Definition of separator: any whitespace (as defined by the standard C library isspace function), a period, a question mark, an exclamation point, a comma, a colon, a semicolon, or EOF Return: the current separator For example, if the text file contains: What! Another useless, stupid, and unnecessary program? Yes; What else?: Try input redirection. /[.]/ /}.!?,;:=+#/ and Reverse is called in the following loop: while ((thisSeparator = Reverse(inFile, 1)) != EOF) cout.put(thisSeparator); the following is displayed: tahW! rehtonA sselesU, diputS, dnA yrassecennU margorP? seY; tahW eslE?: yrT tupnI noitcerideR. [/./] }/.!?,;:/#+= Reverse must: * implement a recursive solution, * not use an array, * display words of any length, * be tested on input file "TestFile2.txt", downloaded from the course web site. Hints: * Place input test file "TestFile2.txt" in the appropriate default location described in the "FILE LOCATION QUESTIONS AND ANSWERS" section of the "ASSIGNMENT SUBMISSION REQUIREMENTS" document. * Note 12.6A of "Advanced C/C++ Notes" illustrates using recursion to reverse all characters on a line and how to keep track of which recursive level is active. * Note 12.6B of "Advanced C/C++ Notes" illustrates returning values from deeper recursive levels. *************************************************************************** ************************ END EXERCISE REQUIREMENT ************************* **************************************************************************/ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING ABOVE IT. **************************************************************************/ /* * TODO: * REPLACE THIS COMMENT WITH YOUR TITLE BLOCK. */ /* * TODO: * REPLACE THIS COMMENT WITH YOUR C++ CODE, PLUS ANYTHING NECESSARY TO * SUPPORT MY TEST CODE BELOW. DO NOT INCLUDE ANY UNNEEDED HEADER FILES! */ /*************************************************************************** * BEFORE PRINTING THIS ASSIGNMENT TO TURN IN, DELETE THIS COMMENT AND * EVERYTHING BELOW IT. **************************************************************************/ /* ***** Change nothing below this comment without instructor permission. **** * Everything that follows was written to help test/verify your code. You * do not need to understand the details of my code to write yours! */ #ifdef ALLOW_INSTRUCTOR_CODE const char * const FILENAME = "TestFile2.txt"; int main() { ifstream inFile; int thisSeparator; OpenFile(FILENAME, inFile); while ((thisSeparator = Reverse(inFile, 1)) != EOF) cout.put(char(thisSeparator)); inFile.close(); return EXIT_SUCCESS; } #endif