All pastes #1648884 Raw Edit

DateValidation

public cpp v1 · immutable
#1648884 ·published 2009-10-30 05:30 UTC
rendered paste body
int main(int argc, char *argv[]){	std::string date_string;	std::cout<<"Enter the date in DD-MM-YYYY format."<<std::endl;	std::cin>>date_string;	int d=std::atoi(date_string.substr(0, 2).c_str());	int m=std::atoi(date_string.substr(3, 2).c_str());	int y=std::atoi(date_string.substr(6, 4).c_str());	int days_of_month[]={31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};	if(m>12 || m<1){		std::cout<<"invalid"<<std::endl;	}else{		int is_leap=(y%4==0) && ((y%100!=0) || (y%100==0 && y%400==0));		if(d<=(days_of_month[m-1]+(m==2? is_leap : 0))){			std::cout<<"The given date is valid."<<std::endl;		}else{			std::cerr<<"The given date is *INVALID* !"<<std::endl;		}	}}/* Sample Run :rahul@kodersbox:~/Desktop$ g++ date_validation.cpp -o date_validationrahul@kodersbox:~/Desktop$ ./date_validationEnter the date in DD-MM-YYYY format.29-02-1000The given date is *INVALID* !*/