I don't know if I should post all the code to start, but if you need to see all the code just ask. Trying to post the relevant stuff:

void findHoliday (const DayData holidayList[],int listLength, 
                     int month,int day, char holidayCopy[]) 
....

holidayCopy = holidayList[i].holiday;

now it's puzzling since the code (a homework template taken from a manual) doesn't initialize the holidayCopy array. The user enters the month and day, and the function does a for/if loop to find a holiday, put the name into holidayCopy.

Here is the error

invalid conversion from ‘const char*’ to ‘char’

holidayCopy is a character array and i think holidayList.holiday is a char variable of the DayData class.

You are trying to assign a character to an array which is illegal.

Maybe this is wat u are lookiing for : holidayCopy[i] = holidayList[i].holiday; Hope this helped,
Bye.

the .holiday field is an array.

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, day;               // Month / day of holiday 
    char holiday[MAX_NAME_LEN];   // Name of holiday 
};
//   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, day;               // Month / day of holiday 
    char holiday[MAX_NAME_LEN];   // Name of holiday 
}; 
 
// Function prototype 
void findHoliday ( const DayData holidayList[], int listLength, 
                   int month, int day, char holidayCopy[]); 
 
int 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("HOLIDAYS.DAT" /*, ios::nocreate */);
     
    if (!holidayFile) { 
         cout << "File cannot be opened.  Program terminates.." << endl; 
         return 0; 
    } 
 
    // 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'); 
        count++;                        
    } 
 
    // 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, holidayName); 
    if ( holidayName[0] != '\0' ) 
       cout << holidayName << endl; 
    else 
       cout << "No holiday listed" << endl;
       
    return 0;
     
} // end main 
 
//-------------------------------------------------------------------- 
// Insert your findHoliday() function here. 
//-------------------------------------------------------------------- 
void findHoliday (const DayData holidayList[],int listLength, 
                     int month,int day, char holidayCopy[])  {
                      
    //  if the month day pair is found in the array of structures 
    //        holidayList, copy the holiday name into holidayCopy 
    //        and return true 
    //  if the month day pair is not found in the array of structures 
    //        holidayList, place '\0' in cell 0 of holidayCopy and 
    //        return false 
 
    for(int i = 0; i < sizeof(holidayList); i++) 
        if(month == holidayList[i].month && day == holidayList[i].day) {
            holidayCopy[0] = holidayList[i].holiday;

        }
        else if(i == sizeof(holidayList) - 1)
            holidayCopy[0] = '\0';
    
    
     
 
 
} // end findHoliday

data file:

1 11 Hostos Day (Puerto Rico)
1 15 Martin Luther King Jr. Day
1 23 Handwriting Day
2 3 Setsubun (Bean-throwing festival in Japan)
2 5 Cham Cham Mapinduzi Day (Tanzania)
2 6 Babe Ruth's Birthday
2 9 Feast of Saint Appolonia (patron saint of dentists)
2 10 Feast of St. Paul's Shipwreck (Malta)
3 31 Bunsen Burner Day
4 22 Earth Day


/*

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


*/

Holidays.cpp: In function ‘void findHoliday(const DayData*, int, int, int, char*)’:
Holidays.cpp:81: error: invalid conversion from ‘const char*’ to ‘char’

*holidayCopy = *holidayList[i].holiday; this compiles, but doesn't work *holidayCopy = holidayList[i].holiday; also compiles but doesn't work

You should use strcpy (char* destination, const char* source) to copy the contents of one char array to another, not the normal assignment stmt "=".

Putting this in place of ur code should do the trick strcpy (holidayCopy, holidayList[i].holiday); Hope it helped,
Bye.

instant delayed gratification! i get stuck on these and can't move to the next problem before getting this. my professor may want it another way but i have plenty of points to work with.

karma++

thanks!

This article has been dead for over six months. Start a new discussion instead.