2017-07-25 1 views
-3

Ich versuche, durch diese Struktur zu gehen, und einen Urlaub auf der Grundlage einer Reihe und Ive versucht alles und es wird nicht funktionieren, um einen Urlaub wie auf 11 zu finden ist ein Urlaub .. Alles ist richtig, außer für die FunktionWie finde ich einen Urlaub auf der Grundlage eines Arrays und Strukturen

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

Bitte geben Sie an, was die Funktion tun soll (wie sie einen Feiertag findet, wie die Parameter verwendet werden sollen ...). Auch müssen wir vielleicht wissen, was DayData Typ ist (wie ist es definiert ...) – HatsuPointerKun

+0

@HatsuPointerKun Ich habe den Rest des Programms – katie

Antwort

0

Sie sollten return, nachdem Sie einen Urlaub zu finden, sonst wird das Ergebnis 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

hinzugefügt, aber warum brauchen Sie eine return-Anweisung, wenn es void sagt? – katie

+0

Das bedeutet, dass Sie die Funktion am Punkt "return" beenden, ohne die folgenden Codes auszuführen. – cycycyc

+0

Oder verwenden Sie 'break' ist klarer. Das bedeutet, dass Sie an der Stelle "Pause" aus der for-Schleife springen, was bedeutet, dass Sie keine anderen Daten in der Liste berücksichtigen, weil Sie die gewünschte gefunden haben. – cycycyc

Verwandte Themen