2017-07-25 54 views
-3

我想通過這個結構,並得到一個基於數組的假期,我試過了一切,它不會工作,找到假期就像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 
+0

請指出該函數應該做什麼(它如何找到假期,參數是如何使用的......)。此外,我們可能需要知道DayData類型是什麼(它是如何定義的......) – HatsuPointerKun

+0

@HatsuPointerKun我添加了程序 – katie

回答

0

你應該return你找到一個假期後,否則結果將overrided。

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); 
     return; // break also works 

    } else { 
      holidayCopy[j] = '\0'; 
     } 
    } 


} // end findHoliday 
+0

的其餘部分,但爲什麼如果它表示無效則需要返回語句? – katie

+0

這意味着你在'return'處終止函數而不運行以下代碼。 – cycycyc

+0

或者使用'break'更清晰。這意味着你跳出了'break'的for循環,這意味着你不會考慮列表中的其他日期,因爲你已經找到了你想要的。 – cycycyc

相關問題