Answer to Question #212214 in C++ for Hemambar

Question #212214

Write a program to create a class Date with day, month and year as members. Write a member

function to validate the dates and display the invalid dates. Test the class with an array of Date objects.


1
Expert's answer
2021-07-03T04:48:28-0400

// A C++ program to check if date is valid

#include<iostream>
using namespace std;
class date
{
	private:
	int day,month,yr;
	public:
	int validate();
};
int validate()
{
	int day,month,yr;
	cout<<"Enter date (In the order DAY MONTH YEAR, please) \n";
  
 cin>>day>>month>>yr;
  
 if(1000 <= yr <= 3000)
    {
     if((month==1 || month==3 || month==5|| month==7|| month==8||month==10||month==12) && day>0 && day<=31)
     cout<<"That is a valid date\n\n";
     else 
   if(month==4 || month==6 || month==9|| month==11 && day>0 && day<=30)
      cout<<"That is a valid date\n\n";
     else
      if(month==2)
        {
        if((yr%400==0 || (yr%100!=0 && yr%4==0)) && day>0 && day<=29)
         cout<<"That is a valid date\n\n";
        else if(day>0 && day<=28)
         cout<<"That is a valid date\n\n";
        else
         cout<<"That is an Invalid date\n\n";
        }
     else
        cout<<"That is an Invalid date\n\n";
   }
  else
    cout<<"That is an Invalid date\n\n";
  
}
int main()
{
	date checkdate;
	cout<<validate();
}

OUTPUT when the date is valid



OUTPUT when the date is Invalid




Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

Hemambar
30.06.21, 10:06

Sir, Why it is not answered

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS