Question: FIX THIS C++ PROGRAM modify to only accepts reasonable values for month and day. The month should be between 1 and 12. The day should
FIX THIS C++ PROGRAM
modify to only accepts reasonable values for month and day. The month should be between 1 and 12. The day should be between 1 and the number of days in the selected month. If input is invalid, give warning and asks again.
And define the default constructor and overloaded constructor.
#include
#include
using namespace std;
// Creating a namespace for an array month and day
namespace day_mon{
//Month Array
string month_list[12] = {"January","February","March","April","May","June","July","August","September","October","November","December"};
//Days array for number of days in the month
int days_in_month[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31,};
}
//Declare the class
class Date{
//Declare priate
private:
int day, month, year;
//Declare public
public:
//Defualt date constructor initializing the default vales for date, month and year
Date(){
//Month should be less than 12 and greater than 1 amd less than days in corresponding month
if ((month >= 1 && month <= 12) && (day <= day_mon::days_in_month[month - 1])){ //If valid date passed than set values
this -> day = 1;
this -> month = 1;
this -> year = 2001;
}
}
//added parameterized constructor
Date(int day,int month,int year){
//Month should be less than 12 and greater than 1 amd less than days in corresponding month
if ((month >= 1 && month <= 12) && (day <= day_mon::days_in_month[month - 1])){ //If valid date passed than set values
this -> day = day;
this -> month = month;
this -> year = year;
}
}
//Print date in format: month/day/last two numbers of year
string format_1()
{
return to_string(month) + "/"
+""+ to_string(day) + "/"
+ to_string(year).substr(2);
}
//Print date in format: month name day, year
string format_2()
{
return day_mon::month_list[month -1]
+" "+ to_string(day)
+", "+ to_string(year);
}
//Print date in format: day month name yeat
string format_3()
{
return to_string(day) + " "
+ day_mon::month_list[month -1]
+ " "+ to_string(year);
}
};
//Class end here
//Main
int main(void) {
//Creating variable for stroring day, month and year
unsigned int day, month, year;
cout << "Enter the day:\t";
// Get and validate the input
while (day < 1|| day > 12 )
{ cout << "Invalid input. Enter an integer between 1 and 4: ";
cin >> day;
}
cout << "Enter the month:\t";
cin >> month;
// Get and validate the input
while (month < 1 || month > 30)
{ cout << "Invalid input. Enter an integer between 1 and 4: ";
cin >> month;
}
cout << "Enter the year: \t";
// Get and validate the input
cin >> year;
while (year < 2021 || year > 2021)
{ cout << "Invalid input. Enter an integer between 1 and 4: ";
cin >> year;
}
}
// Creating defualt object having defualt value for day, month, year
Date d(day, month, year);
//Display date in three formates
cout << d.format_1() << endl;
cout << d.format_2() << endl;
cout << d.format_3() << endl;
cout << endl;
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
