hello!

I need a program that validates the imput of dates. (Checks for leap years etc.)

The user enters: mm/dd/yyyy

The problem is, I have trouble with complex programs. I came up with this program, but i know it's too basic for what she wants. I need to have arrays in there, but i don't know how. I would love any help considering I'm bad at understanding this subject.

:)


could someone help me ???!!!

bool GoodDate(int month,int day,int year)


    {
    	/*** Bad Month ***/
    	if(month < 1 || month > 12)
    		return false;
    	
    	/*** January ***/
    	else if(month == 1)


        	{
        		if(day >=1 && day<= 31)
        			return true;
        		else
        			return false;
        	}
        	
        	/*** Febuary ***/
        	else if(month == 2) // divisible by 4 and either divisible by 400 or not divisible by 100


            	{
            		/*** if the year is a leap year... ***/
            		if(year % 4 == 0 && (year % 400 == 0 || year % 100 != 0))


                		{
                			if(day >= 1 && day <= 29)
                				return true;
                			else
                				return false;
                		}
                		/*** if the year is not a leap year... ***/
                		else

                    		{
                    			if(day >=1 && day <= 28)
                    				return true;
                    			else
                    				return false;
                    		}
                    	}
                    	
                    	/*** March ***/
                    	else if(month == 3)


                        	{
                        		if(day >=1 && day<= 31)
                        			return true;
                        		else
                        			return false;
                        	}
                        	
                        	/*** April ***/	
                        	else if(month == 4)


                            	{
                            		if(day >=1 && day<= 30)
                            			return true;
                            		else
                            			return false;
                            	}
                            	
                            	/*** May ***/	
                            	else if(month == 5)


                                	{
                                		if(day >=1 && day<= 31)
                                			return true;
                                		else
                                			return false;
                                	}
                                	
                                	/*** June ***/	
                                	else if(month == 6)


                                    	{
                                    		if(day >=1 && day<= 30)
                                    			return true;
                                    		else
                                    			return false;
                                    	}
                                    	
                                    	/*** July ***/	
                                    	else if(month == 7)


                                        	{
                                        		if(day >=1 && day<= 31)
                                        			return true;
                                        		else
                                        			return false;
                                        	}
                                        	
                                        	/*** August ***/	
                                        	else if(month == 8)


                                            	{
                                            		if(day >=1 && day<= 31)
                                            			return true;
                                            		else
                                            			return false;
                                            	}
                                            	
                                            	/*** September ***/	
                                            	else if(month == 9)


                                                	{
                                                		if(day >=1 && day<= 30)
                                                			return true;
                                                		else
                                                			return false;
                                                	}
                                                	
                                                	/*** October ***/	
                                                	else if(month == 10)


                                                    	{
                                                    		if(day >=1 && day<= 31)
                                                    			return true;
                                                    		else
                                                    			return false;
                                                    	}
                                                    	
                                                    	/*** November ***/	
                                                    	else if(month == 11)


                                                        	{
                                                        		if(day >=1 && day<= 30)
                                                        			return true;
                                                        		else
                                                        			return false;
                                                        	}
                                                        	
                                                        	/*** December ***/	
                                                        	else if(month == 12)


                                                            	{
                                                            		if(day >=1 && day<= 31)
                                                            			return true;
                                                            		else
                                                            			return false;
                                                            	}
                                                            	                                                            	else

                                                                	{
                                                                		cout<<"Error\n";
                                                                		exit(0);
                                                                	}
                                                                	return false;
                                                            }

Recommended Answers

All 7 Replies

Let me give you this hint - since several of the months have the same behavior - ie 1,3,5,7,8,10,12 are all 31 days, while 4,6,9,11 are all 30 days, then you could easily use a 'switch' statement on your month and not have to repeat all of that logic.

okay I came up with something a little more complex, but it's still not working. I know it's somewhere within my functions (either the bools or whatnot)......basically it's not telling them they entered an invalid date.

Any help?? I didn't put it in there, because I can't figure out where it goes logically.


/

************************Includes**************************************/

# include<iostream>
# include<iomanip>
using namespace std;

/************************Function call*********************************/

int isLeapYear(int year);                                //function
int dateIsValid (int day, int month, int year);          //function


/***********************Main******************************************/

int main()
	{
      int value1, value2, value3;
	  char again;

      do
	  {
			cout << "Enter the day, month, and year. \n";
			cin  >> value1 >> value2 >> value3;
			dateIsValid(value1, value2, value3);               //call function
			cout << value1<<" "<< value2 <<" "<< value3<<endl;
			cout << " Do you want to enter another date? (Y/N)";
			cin >>again;

	  }while (again =='Y' || again =='y');
      
	  return 0;
}

 /*************************FUNCTION*********************************/

    int dateIsValid(int day, int month, int year)

    {
		bool valid;
		int monthLength[13] =  { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    
		 if ( isLeapYear(year) )
             monthLength[2] = 29;    //  29 days in February in a leap year 

		if ( month < 1 || month > 12 )
        	valid = false;
	    else if ( day < 1 || day > monthLength[month] )
        	valid = false;

    return ( valid );

    }
    
 /****************************FUNCTION*******************************************/

    int isLeapYear(int year)
    {
       bool result;
    
       if ( (year%4) != 0 )               //  or:    if ( year%4 )
          result = false;                 //  means: if year is not divisible by 4
       else if ( (year%400) == 0 )        //  or:    if ( !(year%400) )
          result = true;                  //  means: if year is divisible by 400
       else if ( (year%100) == 0 )        //  or:    if ( !(year%100) )
          result = false;                //  means: if year is divisible by 100
       else                              //  (but not by 400, since that case
           result = true;                //  considered already)
    
    return ( result );

    }

put ur whole code so I can find the problem I did the same thing last week for my assignment.

These examples are overly complicated. I found a simple one that totally handles leap years.

from: c++ checkdate date validation

bool checkdate(int m, int d, int y)
{
  //gregorian dates started in 1582
  if (! (1582<= y )  )//comment these 2 lines out if it bothers you
     return false;
  if (! (1<= m && m<=12) )
     return false;
  if (! (1<= d && d<=31) )
     return false;
  if ( (d==31) && (m==2 || m==4 || m==6 || m==9 || m==11) )
     return false;
  if ( (d==30) && (m==2) )
     return false;
  if ( (m==2) && (d==29) && (y%4!=0) )
     return false;
  if ( (m==2) && (d==29) && (y%400==0) )
     return true;
  if ( (m==2) && (d==29) && (y%100==0) )
     return false;
  if ( (m==2) && (d==29) && (y%4==0)  )
     return true;

  return true;
}
commented: useful +11

A few years late, blacklight?

bool data(int dzien,int miesiac,int rok){
int rokp[13] = { 31,29,31,30,31,30,31,31,30,31,30,31 };//tablica przestepna
int rokz[13] = { 31,28,31,30,31,30,31,31,30,31,30,31 };//tablica zwykla

if(rok>=0){//rok
if((rok%4 == 0 && rok%100 != 0) || rok%400 == 0){//przestepny
	if(miesiac<=12){//miesiace
		if(dzien>0 && rokp[miesiac-1]>=dzien){return true;}else{return false;}//dnie
	}
}else{//zwykly
	if(miesiac<=12){//miesiace
		if(dzien>0 && rokz[miesiac-1]>=dzien){return true;}else{return false;}//dnie
	}
}
}//rok
}

Please use English when posting on this formun. Another thing, you do realize this thread died over 2 years ago?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.