| | |
need help w/ 'invalid conversion'
Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
![]() |
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:
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
C++ Syntax (Toggle Plain Text)
void findHoliday (const DayData holidayList[],int listLength, int month,int day, char holidayCopy[]) .... holidayCopy = holidayList[i].holiday;
Here is the error
•
•
•
•
invalid conversion from ‘const char*’ to ‘char’
holidayCopy is a character array and i think holidayList[i].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 :
Hope this helped,
Bye.
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.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
the .holiday field is an array.
C++ Syntax (Toggle Plain Text)
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 };
C++ Syntax (Toggle Plain Text)
// 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
•
•
•
•
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’
You should use
Putting this in place of ur code should do the trick
Hope it helped,
Bye.
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.
I don't accept change; I don't deserve to live.
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
Jo Tujhe Jagaaye, Nindein Teri Udaaye Khwaab Hai Sachcha Wahi.
Nindon Mein Jo Aaye Jise To Bhul Jaaye Khawab Woh Sachcha Nahi.
Khwaab Ko Raag De, Nind Ko Aag De
![]() |
Similar Threads
- Invalid conversions while using other people's software (C++)
- Convert first character from lowercase to uppercase? (C)
- C++ program using classes (C++)
- Is this how to read in char only (C++)
- passing a vector to a function? :errors: (C++)
Other Threads in the C++ Forum
- Previous Thread: String help
- Next Thread: Constrcutors and Destructors
Views: 5136 | Replies: 8
| Thread Tools | Search this Thread |
Tag cloud for C++
6 api application array arrays assignment beginner binary bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion convert count data database delete developer display dll email encryption error file forms fstream function functions game generator getline givemetehcodez graph homeworkhelper iamthwee ifstream image input int java lazy lib loop looping loops map math matrix memory multidimensional multiple newbie news node number output parameter pointer problem program programming project proxy python random read recursion recursive reference return sort sorting string strings struct template templates text tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






