need help w/ 'invalid conversion'

Please support our C++ advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

need help w/ 'invalid conversion'

 
0
  #1
Jul 20th, 2006
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:

  1. void findHoliday (const DayData holidayList[],int listLength,
  2. int month,int day, char holidayCopy[])
  3. ....
  4.  
  5. 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’
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: need help w/ 'invalid conversion'

 
0
  #2
Jul 20th, 2006
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 :

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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: need help w/ 'invalid conversion'

 
0
  #3
Jul 20th, 2006
the .holiday field is an array.

  1. const int MAX_DATES = 60, // Max number of holidays in list
  2. MAX_NAME_LEN = 81; // Max length of holiday name
  3.  
  4. #include <iostream>
  5. #include <fstream>
  6. using namespace::std;
  7.  
  8.  
  9. // Definition of DayData type
  10. struct DayData {
  11.  
  12. int month, day; // Month / day of holiday
  13. char holiday[MAX_NAME_LEN]; // Name of holiday
  14. };
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: need help w/ 'invalid conversion'

 
0
  #4
Jul 20th, 2006
  1. // holidays.cpp
  2. // Finds a holiday for a specified date from a list of holidays.
  3.  
  4. const int MAX_DATES = 60, // Max number of holidays in list
  5. MAX_NAME_LEN = 81; // Max length of holiday name
  6.  
  7. #include <iostream>
  8. #include <fstream>
  9. using namespace::std;
  10.  
  11.  
  12. // Definition of DayData type
  13. struct DayData {
  14.  
  15. int month, day; // Month / day of holiday
  16. char holiday[MAX_NAME_LEN]; // Name of holiday
  17. };
  18.  
  19. // Function prototype
  20. void findHoliday ( const DayData holidayList[], int listLength,
  21. int month, int day, char holidayCopy[]);
  22.  
  23. int main () {
  24.  
  25. DayData holidayList[MAX_DATES]; // List of holidays
  26. int count = 0, // Number of holidays in list
  27. month, // Input month / day
  28. day;
  29. char holidayName[MAX_NAME_LEN]; // Name of selected holiday
  30.  
  31. // Open the designated file for input.
  32. ifstream holidayFile("HOLIDAYS.DAT" /*, ios::nocreate */);
  33.  
  34. if (!holidayFile) {
  35. cout << "File cannot be opened. Program terminates.." << endl;
  36. return 0;
  37. }
  38.  
  39. // Read in the list of holidays.
  40. while (holidayFile.good()
  41. && holidayFile >> holidayList[count].month >> holidayList[count].day )
  42. {
  43. holidayFile.get(); // Remove blank after day from the stream
  44. holidayFile.getline(holidayList[count].holiday, MAX_NAME_LEN,'\n');
  45. count++;
  46. }
  47.  
  48. // Close the file.
  49. holidayFile.close();
  50.  
  51. // Prompt the user for the date of the desired hoilday.
  52. cout << endl << "Enter the month and day for a holiday: ";
  53. cin >> month >> day;
  54.  
  55. // Display the holiday (if any) for the requested date.
  56. findHoliday(holidayList, count, month, day, holidayName);
  57. if ( holidayName[0] != '\0' )
  58. cout << holidayName << endl;
  59. else
  60. cout << "No holiday listed" << endl;
  61.  
  62. return 0;
  63.  
  64. } // end main
  65.  
  66. //--------------------------------------------------------------------
  67. // Insert your findHoliday() function here.
  68. //--------------------------------------------------------------------
  69. void findHoliday (const DayData holidayList[],int listLength,
  70. int month,int day, char holidayCopy[]) {
  71.  
  72. // if the month day pair is found in the array of structures
  73. // holidayList, copy the holiday name into holidayCopy
  74. // and return true
  75. // if the month day pair is not found in the array of structures
  76. // holidayList, place '\0' in cell 0 of holidayCopy and
  77. // return false
  78.  
  79. for(int i = 0; i < sizeof(holidayList); i++)
  80. if(month == holidayList[i].month && day == holidayList[i].day) {
  81. holidayCopy[0] = holidayList[i].holiday;
  82.  
  83. }
  84. else if(i == sizeof(holidayList) - 1)
  85. holidayCopy[0] = '\0';
  86.  
  87.  
  88.  
  89.  
  90.  
  91. } // 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’
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: need help w/ 'invalid conversion'

 
0
  #5
Jul 20th, 2006
*holidayCopy = *holidayList[i].holiday;

this compiles, but doesn't work

*holidayCopy = holidayList[i].holiday; also compiles but doesn't work
Last edited by tefflox; Jul 20th, 2006 at 3:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2006
Posts: 7,651
Reputation: ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of ~s.o.s~ has much to be proud of 
Solved Threads: 474
Super Moderator
Featured Poster
~s.o.s~'s Avatar
~s.o.s~ ~s.o.s~ is offline Offline
Failure as a human

Re: need help w/ 'invalid conversion'

 
0
  #6
Jul 20th, 2006
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.
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
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: need help w/ 'invalid conversion'

 
0
  #7
Jul 20th, 2006
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!
Reply With Quote Quick reply to this message  
Join Date: Jul 2005
Posts: 1,758
Reputation: Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all Lerner is a name known to all 
Solved Threads: 283
Lerner Lerner is offline Offline
Posting Virtuoso

Re: need help w/ 'invalid conversion'

 
0
  #8
Jul 20th, 2006
~sos~ already said it.
Last edited by Lerner; Jul 20th, 2006 at 3:51 pm.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 174
Reputation: tefflox is an unknown quantity at this point 
Solved Threads: 1
tefflox's Avatar
tefflox tefflox is offline Offline
Junior Poster

Re: need help w/ 'invalid conversion'

 
0
  #9
Jul 20th, 2006
what have you said?
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 5136 | Replies: 8
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC