/*****************************************************************************/ /*****************************************************************************/ /*This program reads in a weekly log, consisting of a list of DAY TIME EVENT and then prints the log to the screen */ /*****************************************************************************/ /*****************************************************************************/ #include #include #include using namespace std; const int NUMDAYS = 7; const int MAXWORDLEN = 80; //FindDayNum is given a day as a string and returns the correct number //0 (for sunday) to 6 (for saturday) int FindDayNum(char day[], char days[NUMDAYS][MAXWORDLEN]); //ClearActivities initially sets whether anything happened on a given //weekday to be false void ClearActivities(bool activity[]); /*****************************************************************************/ /*****************************************************************************/ int main() { //FILE variables ifstream infile; char infilename[MAXWORDLEN]; //day is a string for the current day being processed char day[MAXWORDLEN]; //days is the list of all days char days[NUMDAYS][MAXWORDLEN] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"}; //activity stores whether anything happened on a given day //so we don't access unused days from the dayactivity array bool activity[NUMDAYS]; //daytime stores the time for each day when something happened char daytime[NUMDAYS][MAXWORDLEN]; //dayactivity stores what happed on a given day char dayactivity[NUMDAYS][MAXWORDLEN]; int daynum=0; //Get the file read for processing cout << "Please enter the log file name : "; cin >> infilename; infile.open(infilename); if (infile.fail()) { cout << "Failed to open file " << infilename << endl; exit(1); //terminate program } //start all activities as false ClearActivities(activity); //Repeat until the user presses Ctrl-D or enters an invalid day while ((daynum>=0) && (infile>>day)) { daynum = FindDayNum(day, days); if (daynum>=0) //it is a valid day { activity[daynum]=true; ////Read when the activity occurred - replaced cin with infile //cin >> daytime[daynum]; infile >> daytime[daynum]; ////Read what activity occurred - replace cin with infile //cin.getline(dayactivity[daynum], MAXWORDLEN); infile.getline(dayactivity[daynum], MAXWORDLEN); } } //Print all the activities out to the screen for (int d=0; d<7; d++) if (activity[d]) cout << days[d] << " " << daytime[d] << " " << dayactivity[d] << endl; else cout << days[d] << " : Nothing done\n"; infile.close(); return 0; } /*****************************************************************************/ /*****************************************************************************/ //Compares the 1st 2 letters (as lowercase) to determine which day int FindDayNum(char day[], char days[NUMDAYS][MAXWORDLEN]) { int daynum=NUMDAYS-1; while (daynum>=0) { if ((tolower(day[0])==days[daynum][0]) && (tolower(day[1])==days[daynum][1])) { break; //found the day so stop looking } daynum--; } return daynum; } /*****************************************************************************/ /*****************************************************************************/ //Initialize all days as no activities void ClearActivities(bool activity[]) { for (int i=0; i