#include "stdstuff.h" #include "Date.h" struct vacationInfo { String2002 description, keywords; Date departDate, returnDate; }; // Read in the vacation data. (This is mostly copied from assignment #1.) bool readVacationData (vacationInfo vacations[], int maxVacations, int &actualVacations) { const int FILENAMESIZE = 60; char filename [FILENAMESIZE]; ifstream fin; String2002 description; cout << "Enter filename for data file: "; cin >> setw(FILENAMESIZE) >> filename; fin.open (filename); if (fin.fail()) { cout << "Unable to open file <" << filename << ">.\n"; return false; } actualVacations = 0; // none so far for (;;) { fin >> ws; // skip whitespace getline (fin, description, '\n'); if (fin.fail()) { if (fin.eof()) { return true; // normal end of reads } else { cout << "Error in reading data file (after data for " << actualVacations << " vacations successfully read.\n"; return false; } } if (actualVacations == maxVacations) { cout << "Insufficient space for vacation data.\n"; return false; } vacations[actualVacations].description = description; getline (fin, vacations[actualVacations].keywords, '\n'); vacations[actualVacations].departDate.read (fin); vacations[actualVacations].returnDate.read (fin); if (fin.fail()) { cout << "Error in additional data for\n<" << vacations[actualVacations].description << ">\n"; return false; } actualVacations++; } } // sees if the given vacation fits the criteria specified. Returns true if it does // and false otehrwise. bool vacationFitsCriteria (const vacationInfo &vacation, int minDays, int maxDays, const Date &firstDate, const Date &lastDate, const String2002 &keyword) { int length; length = 1 + (vacation.returnDate.daysAfter(vacation.departDate)); if ((length < minDays) || (length > maxDays)) { return false; // vacation to short or too long } if ( (vacation.departDate.daysAfter(firstDate) < 0) || (lastDate.daysAfter(vacation.returnDate) < 0) ) { // vacation depart date is before earliest possible departure, or // lastest possible return is before vacation return date return false; } if (keyword == "NONE") { // no keyword critera return true; } // vacation fits criteria if and only if keyword exists return vacation.keywords.find(keyword)!= String2002::npos; } // search the vacations to see which fit the criteria. Counts the ones that match // the criteria and prints them all out along with the total. void doVacationSearch (const vacationInfo vacations[], int actualVacations, int minDays, int maxDays, const Date &firstDate, const Date &lastDate, const String2002 &keyword) { int i, count = 0; // number of vacations found for (i = 0; i < actualVacations; i++) { if (vacationFitsCriteria (vacations[i], minDays, maxDays, firstDate, lastDate, keyword)) { // it fits the criteria so print the information on this vacation and count it cout << "\n" << vacations[i].description << "\n" << vacations[i].keywords << "\nDeparture: "; vacations[i].departDate.write (cout); cout << " Return: "; vacations[i].returnDate.write (cout); cout << "\n"; count++; } } // print out total or that there were no vacations matching the criteria if (count == 0) { cout << "\nSorry - No vacations match those criteria.\n"; } else { cout << "\n" << count << " vacations found.\n"; } } int main () { const int MAXVACATIONS = 100; vacationInfo vacations[MAXVACATIONS]; int actualVacations, minDays, maxDays; Date firstDate, lastDate; String2002 keyword; if (!readVacationData (vacations, MAXVACATIONS, actualVacations)) { cout << "Unable to read in vacation information.\n"; pause (); return 0; } for (;;) { cout << "\nPlease enter minimum and maximum duration (0 0 to stop): "; cin >> minDays >> maxDays; if ((minDays == 0) && (maxDays == 0)) { pause(); return 0; } cout << "Please enter earliest possible departure date (DD MM YYYY): "; firstDate.read (cin); cout << "Please enter latest possible return date (DD MM YYYY): "; lastDate.read (cin); cout << "Please enter keyword (NONE if none required): "; cin >> keyword; if (cin.fail()) { cin.clear(); cin.ignore(INT_MAX, '\n'); cout << "\nAn input error occured - please try again.\n"; } else { doVacationSearch (vacations, actualVacations, minDays, maxDays, firstDate, lastDate, keyword); } } }