1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
|
// holidays.cpp
// Finds a holiday for a specified date from a list of holidays.
const int MAX_DATES = 60, // Max number of holidays in list
MAX_NAME_LEN = 81; // Max length of holiday name
#include <iostream>
#include <fstream>
using namespace::std;
// Definition of DayData type
struct DayData
{
int month, // Month / day of holiday
day;
char holiday[MAX_NAME_LEN]; // Name of holiday
};
// Function prototype
void findHoliday ( const DayData holidayList[], int listLength,
int month, int day, char holidayCopy[] );
void main ()
{
DayData holidayList[MAX_DATES]; // List of holidays
int count = 0, // Number of holidays in list
searchMonth, // Input month / day
searchDay;
char holidayName[MAX_NAME_LEN]; // Name of selected holiday
// Open the designated file for input.
ifstream holidayFile(" holidays.txt");
if (!holidayFile) {
cout << "Can NOT open file " << endl;
return;
}
// Read in the list of holidays.
while (holidayFile.good() && holidayFile >>
holidayList[count].month>> holidayList[count].day )
{
holidayFile.get(); // Remove blank after day from the stream
holidayFile.getline(holidayList[count].holiday,
MAX_NAME_LEN,'\n'); // Read holiday name
count++; // including spaces
}
// Close the file.
holidayFile.close();
// Prompt the user for the date of the desired hoilday.
cout << endl << "Enter the month and day for a holiday: ";
cin >> searchMonth >> searchDay;
// Display the holiday (if any) for the requested date.
findHoliday(holidayList,count,searchMonth,searchDay,holidayName);
if ( holidayName[0] != '\0' )
cout << holidayName << endl;
else
cout << "No holiday listed" << endl;
}
//--------------------------------------------------------------------
// Insert your findHoliday() function here.
//--------------------------------------------------------------------
void findHoliday (const DayData holidayList[],int listLength,
int month,int day,char holidayCopy[])
{
} // end findHoliday
/******** Sample Output
Enter the month and day for a holiday: 3 31
Bunsen Burner Day
Enter the month and day for a holiday: 2 4
No holiday listed
*/
|