////////////////////////////////////////////////////////////////// // // email2.cpp // // This program asks the user for the name of an e-mail // file, and then alternately: // // (1) prints the numbered list of e-mail messages // // (2) asks the user for a message number and // prints that message // // until the user types 0. // // REVISION HISTORY: // 1/16/01 (Jeff Ondich) Wrote the main program, // OpenEmailFile, and stubs for the rest of // the functions. // ////////////////////////////////////////////////////////////////// // This program uses files, so we need fstream instead of iostream. // Note that fstream implies iostream (so you can still use // cin and cout). #include #include ////////////////////////////////////////////////////// // Function prototypes. ////////////////////////////////////////////////////// void OpenEmailFile( ifstream& inputFile, string fileName ); int GetSelection(); void PrintMessageList( ifstream& inputFile ); void PrintMessage( ifstream& inputFile, int messageNumber ); ////////////////////////////////////////////////////// // main program ////////////////////////////////////////////////////// int main() { // Get the e-mail file's name from the user. string fileName; cout << "Please type the name of your e-mail file: "; cin >> fileName; // Go around and around, printing the numbered list // of messages, asking the user for a message number, // and printing the requested message. When the // user types any number less than or equal to 0, stop. ifstream inputFile; int selection = 1; while( selection > 0 ) { // Print the message list. OpenEmailFile( inputFile, fileName ); PrintMessageList( inputFile ); inputFile.close(); // Get the user's selection. selection = GetSelection(); // Print the selected message. if( selection > 0 ) { OpenEmailFile( inputFile, fileName ); PrintMessage( inputFile, selection ); inputFile.close(); // Make it easier for the user to read the message // before moving on. cout << "Hit return when you're ready for the list again." << endl; cin.ignore(); cin.ignore(); } } return( 0 ); } ////////////////////////////////////////////////////// // Opens an input stream corresponding to // the file of the given name. If the file can't // be opened, we give the user a descriptive // error message and quit the program. ////////////////////////////////////////////////////// void OpenEmailFile( ifstream& inputFile, string fileName ) { inputFile.open( fileName.c_str() ); if( !(inputFile.is_open()) ) { cout << "An error occurred during the attempt to open " << fileName << "." << endl << "Perhaps the file does not exist." << endl; exit( 1 ); } } ////////////////////////////////////////////////////// // Prompts the user for a message number and returns // the user's response. The prompt should explain // that the choice 0 will quit the program. ////////////////////////////////////////////////////// int GetSelection() { // This is a "stub." That is, it's a function // that compiles, but that doesn't have any // code in it yet so it doesn't do its job. // Since it compiles, however, you can test // the other parts of the program. cout << "Greetings from GetSelection()." << endl; return( 0 ); } ////////////////////////////////////////////////////// // Scans through inputFile, printing a numbered // list of the "From:" and "Subject:" lines. // Specifically, you should print out a number // whenever you detect a "From " line, and then // just print every "From:" and "Subject:" line. // // Precondition: inputFile is open, and can thus be // used in input statements just like cin. ////////////////////////////////////////////////////// void PrintMessageList( ifstream& inputFile ) { // Another stub. cout << "PrintMessageList() says 'Howdy.'" << endl; } ////////////////////////////////////////////////////// // Scans through the inputFile, counting messages // (that is, counting "From " lines, of which there // is one per message). While the message count // is equal to messageNumber, just print out // every line. // // Precondition: inputFile is open, and can thus be // used in input statements just like cin. ////////////////////////////////////////////////////// void PrintMessage( ifstream& inputFile, int messageNumber ) { cout << "PrintMessage() got called with " << messageNumber << endl; }