I keep getting an error message when trying to compile my C

I keep getting an error message when trying to compile my C++ code can someone help please?

[Warning] deprecated conversion from string constant to \'char*\' [-Wwrite-strings]

Source code:

#include <iostream>
#include <cstdlib>
#include <string>


//gets thrown
class InvalidDay{
   public:
       print(){
           std::cout << \"The day should be between 1 and 31\";
       }
};


//gets thrown
class InvalidMonth{
   public:
       print(){
           std::cout << \"The month should be between 1 and 12\";
       }
};

class Date{
  
  
   private:
       int month;
       int day;
       int year;
      
   public:
      
       Date(){   };
   //constructs the date object
   /*the control structures ensure that the day and month are within
   acceptable limits, otherwise the error class is thrown*/
  
   Date(int month, int day, int year){
      
       if(day >= 1 && day <= 31) setDay(day);
       else
       throw InvalidDay();
      
       if(month >= 1 && month <= 12) setMonth(month);
       else
       throw InvalidMonth();
      
       setYear(year);
   }      
      
       //get month value
       int getMonth(){return month;}
      
       //set month value
       void setMonth(int m){
           month = m;
       }
      
       //get day value
       int getDay(){return day;}
      
       //set day value
       void setDay(int d){
           day = d;
       }
      
       //get year value
       int getYear(){return year;}
      
       //set year value
       void setYear(int y){
           year = y;
       }
      
       char* convert(int month){
           switch(month){
               //operator << is overloaded for char* to output strings
               // (I did not know this)
               case 1: return \"January\";
               case 2: return \"Febuary\";
               case 3: return \"March\";
               case 4: return \"April\";
               case 5: return \"May\";
               case 6: return \"June\";
               case 7: return \"July\";
               case 8: return \"August\";
               case 9: return \"Septeber\";
               case 10: return \"October\";
               case 11: return \"November\";
               case 12: return \"December\";
              
               default: return NULL;
           }
       }
      
       //print methods
       void printDate(){
           std::cout << getMonth() << \'/\' << getDay() << \'/\' << getYear()
                       << std :: endl;
       }
      
       void printDate1(){
           std::cout << convert(getMonth()) << \" \" << day <<\", \" << year
                       << std :: endl;
       }
      
       void printDate2(){
           std:: cout << day << \" \" << convert(getMonth()) << \" \" << year
                       << std :: endl;
       }
};

int main(){
  
   //int values to be passed to constructor
   int month, day, year;
   std::cout << \"Enter the month day and year\" << std::endl;
  
   std::cin >> month >> day >> year;
  
   //try-catch block catches an invalid day/month when the object is created
  
   try{
       Date date(month, day, year);
      
       date.printDate();
       date.printDate1();
       date.printDate2();
      
   }
  
   catch(InvalidDay d){
       d.print();
      
   }
  
   catch(InvalidMonth m){
       m.print();
   }
  
   return(0);
  
};

Solution

//Errors has been removed and code has been tested on gcc compiler

#include <iostream>
#include <cstdlib>
#include <string>

//gets thrown
class InvalidDay{
public:
void print(){ //Added the return type void for print()
std::cout << \"The day should be between 1 and 31\";
}
};

//gets thrown
class InvalidMonth{
public:
void print(){ //Added the return type void for print()
std::cout << \"The month should be between 1 and 12\";
}
};
class Date{
  
  
private:
int month;
int day;
int year;
  
public:
  
Date(){ };
//constructs the date object
/*the control structures ensure that the day and month are within
acceptable limits, otherwise the error class is thrown*/
  
Date(int month, int day, int year){
  
if(day >= 1 && day <= 31) setDay(day);
else
throw InvalidDay();
  
if(month >= 1 && month <= 12) setMonth(month);
else
throw InvalidMonth();
  
setYear(year);
}
  
//get month value
int getMonth(){return month;}
  
//set month value
void setMonth(int m){
month = m;
}
  
//get day value
int getDay(){return day;}
  
//set day value
void setDay(int d){
day = d;
}
  
//get year value
int getYear(){return year;}
  
//set year value
void setYear(int y){
year = y;
}
  
char* convert(int month){
switch(month){
//operator << is overloaded for char* to output strings
// (I did not know this)
case 1: return (char*)\"January\"; //use the cast operator(char*)
case 2: return (char*)\"Febuary\"; //use the cast operator(char*)
case 3: return (char*)\"March\"; //use the cast operator(char*)
case 4: return (char*)\"April\"; //use the cast operator(char*)   
case 5: return (char*)\"May\"; //use the cast operator(char*)
case 6: return (char*)\"June\"; //use the cast operator(char*)
case 7: return (char*)\"July\"; //use the cast operator(char*)
case 8: return (char*)\"August\"; //use the cast operator(char*)
case 9: return (char*)\"Septeber\"; //use the cast operator(char*)
case 10: return (char*)\"October\"; //use the cast operator(char*)
case 11: return (char*)\"November\"; //use the cast operator(char*)
case 12: return (char*)\"December\"; //use the cast operator(char*)
  
default: return NULL;
}
}
  
//print methods
void printDate(){
std::cout << getMonth() << \'/\' << getDay() << \'/\' << getYear()
<< std :: endl;
}
  
void printDate1(){
std::cout << convert(getMonth()) << \" \" << day <<\", \" << year
<< std :: endl;
}
  
void printDate2(){
std:: cout << day << \" \" << convert(getMonth()) << \" \" << year
<< std :: endl;
}
};
int main(){
  
//int values to be passed to constructor
int month, day, year;
std::cout << \"Enter the month day and year\" << std::endl;
  
std::cin >> month >> day >> year;
  
//try-catch block catches an invalid day/month when the object is created
  
try{
Date date(month, day, year);
  
date.printDate();
date.printDate1();
date.printDate2();
  
}
  
catch(InvalidDay d){
d.print();
  
}
  
catch(InvalidMonth m){
m.print();
}
  
return(0);
  
};

*****Explanation***********
In c++,you can\'t convert string directly to char,you need to use cast operator,I have added my comment whereever i have done the changes.
We need to add the return type of method,for the print method it was missing,i have added return type void for that.

After removing these errors getting following output:
Enter the month day and year
12
11
2016
12/11/2016
December 11, 2016   
11 December 2016

*****Explanation***********

Note:According to the question i have just removed the errors now it is compiling smoothly,i have not done any changes in the logic of the code
Please let me know in case of any doubt,Thanks.

I keep getting an error message when trying to compile my C++ code can someone help please? [Warning] deprecated conversion from string constant to \'char*\' [-
I keep getting an error message when trying to compile my C++ code can someone help please? [Warning] deprecated conversion from string constant to \'char*\' [-
I keep getting an error message when trying to compile my C++ code can someone help please? [Warning] deprecated conversion from string constant to \'char*\' [-
I keep getting an error message when trying to compile my C++ code can someone help please? [Warning] deprecated conversion from string constant to \'char*\' [-
I keep getting an error message when trying to compile my C++ code can someone help please? [Warning] deprecated conversion from string constant to \'char*\' [-
I keep getting an error message when trying to compile my C++ code can someone help please? [Warning] deprecated conversion from string constant to \'char*\' [-

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site