我想通過這個結構,並得到一個基於數組的假期,我試過了一切,它不會工作,找到假期就像1 11是假期.. 一切都是正確的,除了功能如何找到一個基於數組和結構的假期
const int MAX_DATES = 60, // Max number of holidays in list 60
MAX_NAME_LEN = 81; // Max length of holiday name 81
#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("D:\\c++ class week
2\\final\\ConsoleApplication37\\ConsoleApplication37\\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;
}
void findHoliday(const DayData holidayList[], int listLength,
int month, int day, char holidayCopy[])
{
int i;
int j = 0;
for (i = 0; i < listLength; i++) { // for how many elements are in the array
if ((holidayList[i].month == month) && (holidayList[i].day == day)) {
// this line is the problem line below
strcpy(holidayCopy, holidayList[i].holiday);
} else {
holidayCopy[j] = '\0';
}
}
} // end findHoliday
請指出該函數應該做什麼(它如何找到假期,參數是如何使用的......)。此外,我們可能需要知道DayData類型是什麼(它是如何定義的......) – HatsuPointerKun
@HatsuPointerKun我添加了程序 – katie