//****************************************************************** // ConvertDates program // This program reads dates in American form from an input file and // writes them to an output file in American, British, and ISO form. // No data validation is done on the input file //****************************************************************** #include #include // For setw() #include // For file I/O #include // For string type using namespace std; void Get2Digits( ifstream&, string& ); void GetYear( ifstream&, string& ); void OpenForInput( ifstream& ); void OpenForOutput( ofstream& ); void Write( ofstream&, string, string, string ); int main() { string month; // Both digits of month string day; // Both digits of day string year; // Four digits of year ifstream dataIn; // Input file of dates ofstream dataOut; // Output file of dates OpenForInput(dataIn); OpenForOutput(dataOut); if ( !dataIn || !dataOut ) // Make sure files return 1; // were opened dataOut << setw(20) << "American Format" // Write headings << setw(20) << "British Format" << setw(20) << "ISO Format" << endl << endl; Get2Digits(dataIn, month); // Priming read while (dataIn) // While not EOF... { Get2Digits(dataIn, day); GetYear(dataIn, year); Write(dataOut, month, day, year); Get2Digits(dataIn, month); } return 0; } //****************************************************************** void OpenForInput( /* inout */ ifstream& someFile ) // File to be // opened // Prompts the user for the name of an input file // and attempts to open the file // Postcondition: Exercise // // Note: // Upon return from this function, the caller must test // the stream state to see if the file was successfully opened { string fileName; // User-specified file name cout << "Input file name: "; cin >> fileName; someFile.open(fileName.c_str()); if ( !someFile ) cout << "** Can't open " << fileName << " **" << endl; } //****************************************************************** void OpenForOutput( /* inout */ ofstream& someFile ) // File to be // opened // Prompts the user for the name of an output file // and attempts to open the file // Postcondition: Exercise // // Note: // Upon return from this function, the caller must test // the stream state to see if the file was successfully opened { string fileName; // User-specified file name cout << "Output file name: "; cin >> fileName; someFile.open(fileName.c_str()); if ( !someFile ) cout << "** Can't open " << fileName << " **" << endl; } //****************************************************************** void Get2Digits( /* inout */ ifstream& dataIn, // Input file /* out */ string& twoChars ) // Two digits // Reads characters up to a slash from dataIn and returns two // digit characters in the string twoChars. If only one digit // is found before the slash, a leading '0' is inserted. // (If input fails due to end-of-file, twoChars is undefined.) // Precondition: Exercise // Postcondition: Exercise { char firstChar; // First character of a two-digit value char secondChar; // Second character of value char dummy; // To consume the slash, if necessary dataIn >> firstChar; if ( !dataIn ) // Check for EOF return; // If so, exit the function dataIn >> secondChar; if (secondChar == '/') { secondChar = firstChar; firstChar = '0'; } else dataIn >> dummy; // Consume the slash twoChars = firstChar; twoChars = twoChars + secondChar; } //****************************************************************** void GetYear( /* inout */ ifstream& dataIn, // Input file /* out */ string& year ) // Four digits // of year // Reads characters from dataIn and returns four digit characters // in the year string // Precondition: Exercise // Postcondition: Exercise { char digitChar; // One digit of the year int loopCount; // Loop control variable year = ""; loopCount = 1; while (loopCount <= 4) { dataIn >> digitChar; year = year + digitChar; loopCount++; } } //****************************************************************** void Write( /* inout */ ofstream& dataOut, // Output file /* in */ string month, // Month string /* in */ string day, // Day string /* in */ string year ) // Year string // Writes the date represented by month, day, and year to file // dataOut in American, British, and ISO form // Precondition: Exercise // Postcondition: Exercise { dataOut << setw(9) << month << '/' << day << '/' << year; dataOut << setw(13) << day << '/' << month << '/' << year; dataOut << setw(16) << year << '-' << month << '-' << day << endl; }