here is the code:
// 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
month, // Input month / day
day;
char holidayName[MAX_NAME_LEN]; // Name of selected holiday
// Open the designated file for input.
ifstream holidayFile("G:\\C++ Lab Hints\\Lab#12\\holidays.dat");
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 >> month >> day;
// Display the holiday (if any) for the requested date.
findHoliday(holidayList,count,month,day,holidayNam e);
if ( holidayName[0] != '\0' )
cout << holidayName << endl;
else
cout << "No holiday listed" << endl;
return;
}
//--------------------------------------------------------------------
// Insert your findHoliday() function here.
//--------------------------------------------------------------------
void findHoliday (const DayData holidayList[],int listLength,
int month,int day,char holidayCopy[])
{
int i;
int j;
for ( i = 0; i < listLength; i++ )
{
if ( holidayList[i].month == month && holidayList[i].day == day )
{
for ( j = 0; j < MAX_NAME_LEN ; j++ )
{
holidayCopy[j] = holidayList[i].holiday[j];
}
}
}
holidayCopy[j]= '\0';
here is the output:
Enter the month and day for a holiday: 1 11
Hostos Day (Puerto Rico)
Press any key to continue . . .
there is a .dat file that i get the input from linked to the program and the error is get is:
Run-Time Error Check # 2 stack around the variable 'holidayName' is corrupt
what do i need to do to correct this?