Hello Everyone,

I'm new to the forum, currentely I'm taking the basic C++ class and struggle through, it has been over 20 years since write any codes. I have some trouble with the code below, it work fine if I don't bother to check for leap year, but the assignmnet required check the for leap year divisible by 4 then day range from 1-29 and year is February. I put the part for leapyear checker, then it did not work, can anyone help see what I did wrong?? Part that I highlighted has been added then the code crash, please help.

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int lpyr);
	
struct // struct declaration
{
	int Month;
	int Day;
	int Year;
	
}date;

int main()
{
		      				
	// Intialize Array For Number Of Days In Each Month 
	int months[12]= {31,29,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
	//Initialize array for number of month in the year
	const unsigned int string_month = 12;
	string arr[string_month] = {"January","february","March","April","May","June","July","August","September","October","November","December"}; 
	                                                                            
	int mm, dd, yyyy, lpyr = 0;
	
	cout << "This program will display date and convert number of month to string" << endl;
	//Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
	cout << "Enter day in the form of dd: ";
	cin >> dd;//Get day from input
	cout << "Enter year in the form of yyyy: ";
	cin >> yyyy;//Get year from input
	cin.get();

    if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
		{
         cout << "INVALID month: Please try again." << endl;
		 cin.get();
		 exit (1);
         }
         
    else if( dd < 1 || dd > 31 )// Validate to see if input for day is valid
		{
         cout << "INVALID day: Please try again." << endl;
		 cin.get();
		 exit (1);
		}
	else
		cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
		/*{
    if(lpyr % 4 != 0)
        return false;
    else if(lpyr % 100 != 0)
        return true;
    else if(lpyr % 400 != 0)
        return false;
    else 
        return true;		}*/
	
		 dd = dd + 1;// Increase the number of days in feb to 29
         cout << yyyy << " is a leap year and it is should be." << endl;
		 	
		 {		
			date.Year = yyyy;
			date.Month = mm;
			date.Day = dd;

			if(dd <= months[mm -1])
			
				cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
			else
				cout << "Invalid Date for the month!";
		 }		

cin.get();
return 0;
}

Once you get the year. Look if the remainder when divided by 4 is zero.

if(yyyy%4=0)
{
then //code for leap year;
}
else{
//code for normal years.

Secondly.
Instead of checking between 1 to 31 . You should check for values between 1 and the number of days.

Something like this .

if( dd < 1 || dd > (mm-1) )

I guess this will do fine.

line 47: get rid of the else. And check if dd is not greater than the number of days in the month, such as if( dd < 1 || dd > months[dd-1] ) line 65: delete it. Do not do that whether its a leap year or not. The only reason for checking for leap year is to determine whether the max days in Feb is 28 or 29.

You didn't post that leapyear() function which I guess is supposed to determine if the year is a leap year or not.

Hello Ancient Dragon,

Want to thank you very much for all your help, I was born in the year of dragon I guess I'm very slow, I have added the lines you have reconmended and so far so good. One warning related to "lpyr" :warning C4101: 'lpyr' : unreferenced local variable

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int lpyr);
	
struct // struct declaration
{
	int Month;
	int Day;
	int Year;
	
}date;

int main()
{
		      				
	// Intialize Array For Number Of Days In Each Month 
	int months[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
	//Initialize array for number of month in the year
	const unsigned int string_month = 12;
	string arr[string_month] = {"January","february","March","April","May","June","July","August","September","October","November","December"}; 
	                                                                            
	int mm, dd, yyyy, lpyr, lp_ck = 0;
	
	cout << "This program will display date and convert number of month to string" << endl;
	//Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
	cout << "Enter day in the form of dd: ";
	cin >> dd;//Get day from input
	cout << "Enter year in the form of yyyy: ";
	cin >> yyyy;//Get year from input
	cin.get();

    if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
		{
         cout << "INVALID month: Please try again." << endl;
		 cin.get();
		 exit (1);
         }
         
    if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid
		{
         cout << "INVALID day: Please try again." << endl;
		 cin.get();
		 exit (1);
		}

		lp_ck = yyyy%4; //Check If The Year Is A Leap Year Or Not 
	if(lp_ck=0)
		months[1] = 29; 
	else
		months[1] = 28;		cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
		cout << yyyy << " is a leap year and it is should be." << endl;
		 	
		 {		
			date.Year = yyyy;
			date.Month = mm;
			date.Day = dd;

			if(dd <= months[mm -1])
			
				cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
			else
				cout << "Invalid Date for the month!";
		 }		

cin.get();
return 0;
}

Please tell me what do I need to do with leapyear() function, I know I need to do something with this function, I need to declare it or post it , but I do not know where in the code that I supost to do this.

Best Regards,

gazoo

line 47: get rid of the else. And check if dd is not greater than the number of days in the month, such as if( dd < 1 || dd > months[dd-1] ) line 65: delete it. Do not do that whether its a leap year or not. The only reason for checking for leap year is to determine whether the max days in Feb is 28 or 29.

You didn't post that leapyear() function which I guess is supposed to determine if the year is a leap year or not.

First, the code you posted in red isn't needed.

if( leapyear(year) )
   cout << "is a leap year\n";
else
   cout << "is not a leap year\n";

bool leapyear( int year)
{
   // its actually a little more complicated than this but will do for
   // years in the 20th and 21st centuries.
   return (year % 4) == 0 ? true : false;
}

Hi Ancient Dragon;

I changes the line highlighted and I'm not sure if I need semicolon after the function :bool leapyear( int year) ??? it seem to complained :error C2143: syntax error : missing ';' before '<class-head>', maybe I have more then one return??. Please explain what is this line does: return (year % 4) == 0 ? true : false;. I understand return(year%4)==0, but nerver see the ( ? ) and ( true:fal; ) before. Thank in advance.

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int year)
	
struct // struct declaration
{#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int year)
	
struct // struct declaration
{ <<<<<<<<<<<< error C2143: syntax error : missing ';' before '<class-head>'
	int Month;
	int Day;
	int Year;
	
}date;

int main()
{
		      				
	// Intialize Array For Number Of Days In Each Month 
	int months[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
	//Initialize array for number of month in the year
	const unsigned int string_month = 12;
	string arr[string_month] = {"January","february","March","April","May","June","July","August","September","October","November","December"}; 
	                                                                            
	int mm, dd, yyyy, year = 0;
	
	cout << "This program will display date and convert number of month to string" << endl;
	//Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
	cout << "Enter day in the form of dd: ";
	cin >> dd;//Get day from input
	cout << "Enter year in the form of yyyy: ";
	cin >> yyyy;//Get year from input
	cin.get();

    if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
		{
         cout << "INVALID month: Please try again." << endl;
		 cin.get();
		 exit (1);
         }
         
    if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid
		{
         cout << "INVALID day: Please try again." << endl;
		 cin.get();
		 exit (1);
		}

	if(leapyear(year))
		cout << "is a leap year\n";
	else
		cout << "is not a leap year\n";
		cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
		cout << yyyy << " is a leap year and it is should be." << endl;
		 	
		 {		
			date.Year = yyyy;
			date.Month = mm;
			date.Day = dd;

			if(dd <= months[mm -1])
			
				cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
			else
				cout << "Invalid Date for the month!";
			return (year % 4) == 0 ? true : false;
		 }		
cin.get();
return 0;
}
	int Month;
	int Day;
	int Year;
	
}date;

int main()
{
		      				
	// Intialize Array For Number Of Days In Each Month 
	int months[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
	//Initialize array for number of month in the year
	const unsigned int string_month = 12;
	string arr[string_month] = {"January","february","March","April","May","June","July","August","September","October","November","December"}; 
	                                                                            
	int mm, dd, yyyy, year = 0;
	
	cout << "This program will display date and convert number of month to string" << endl;
	//Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
	cout << "Enter day in the form of dd: ";
	cin >> dd;//Get day from input
	cout << "Enter year in the form of yyyy: ";
	cin >> yyyy;//Get year from input
	cin.get();

    if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
		{
         cout << "INVALID month: Please try again." << endl;
		 cin.get();
		 exit (1);
         }
         
    if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid
		{
         cout << "INVALID day: Please try again." << endl;
		 cin.get();
		 exit (1);
		}

	if(leapyear(year))
		cout << "is a leap year\n";
	else
		cout << "is not a leap year\n";

		cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
		cout << yyyy << " is a leap year and it is should be." << endl;
		 	
		 {		
			date.Year = yyyy;
			date.Month = mm;
			date.Day = dd;

			if(dd <= months[mm -1])
			
				cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
			else
				cout << "Invalid Date for the month!";
			return (year % 4) == 0 ? true : false;
		 }		
cin.get();
return 0;
}

error C2143: syntax error : missing ';' before '<class-head>'

First, the code you posted in red isn't needed.

if( leapyear(year) )
   cout << "is a leap year\n";
else
   cout << "is not a leap year\n";

bool leapyear( int year)
{
   // its actually a little more complicated than this but will do for
   // years in the 20th and 21st centuries.
   return (year % 4) == 0 ? true : false;}

Sorry what is that mean??

you double posted the code.

Sorry what is that mean??

Re-read the code you posted -- its there twice. Repost and this time preview your post before hitting the Submit button.

I see and very sorry,

Highlighted I have change the code as reconmended, I have error: [error]
error LNK2019: unresolved external symbol "bool __cdecl leapyear(int)" (?leapyear@@YA_NH@Z) referenced in function _main
[error]

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int year);
	
struct // struct declaration
{
	int Month;
	int Day;
	int Year;
	
}date;

int main()
{
		      				
	// Intialize Array For Number Of Days In Each Month 
	int months[12]= {31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
	//Initialize array for number of month in the year
	const unsigned int string_month = 12;
	string arr[string_month] = {"January","february","March","April","May","June","July","August","September","October","November","December"}; 
	                                                                            
	int mm, dd, yyyy, year = 0;
	
	cout << "This program will display date and convert number of month to string" << endl;
	//Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
	cout << "Enter day in the form of dd: ";
	cin >> dd;//Get day from input
	cout << "Enter year in the form of yyyy: ";
	cin >> yyyy;//Get year from input
	cin.get();

    if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
		{
         cout << "INVALID month: Please try again." << endl;
		 cin.get();
		 exit (1);
         }
         
    if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid
		{
         cout << "INVALID day: Please try again." << endl;
		 cin.get();
		 exit (1);
		}

	if(leapyear(year))

		cout << "is a leap year\n";
	else
		cout << "is not a leap year\n";
		cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
		cout << yyyy << " is a leap year and it is should be." << endl;
		return (year % 4 == 0);	 	
		 {		
			date.Year = yyyy;
			date.Month = mm;
			date.Day = dd;

			if(dd <= months[mm -1])
			
				cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
			else
				cout << "Invalid Date for the month!";
			
		 }		
cin.get();
return 0;
}

Re-read the code you posted -- its there twice. Repost and this time preview your post before hitting the Submit button.

Hi Ancient Dragon,

I'm very close get this code working, it compiled and work fine but if entered 02-28-2004, then 2004 is a leapyear. If it is leapyear then it should display February, 29 2004 instead?? I thing I missing this part in the code.

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int year);
	
struct // struct declaration
{
	int Month;
	int Day;
	int Year;
	
}date;

int main()
{
		      				
	// Intialize Array For Number Of Days In Each Month 
	int months[13]= {31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
	//Initialize array for number of month in the year
	const unsigned int string_month = 12;
	string arr[string_month] = {"January","february","March","April","May","June","July","August","September","October","November","December"}; 
	                                                                            
	int mm, dd, yyyy, year = 0;
	
	cout << "This program will display date and convert number of month to string" << endl;
	//Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
  if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
		{
         cout << "INVALID month: Please try again." << endl;
		 cin.get();
		 system ("pause");
		 exit (1);
         }
	cout << "Enter day in the form of dd: ";
	cin >> dd;//Get day from input
  if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid
		{
         cout << "INVALID day: Please try again." << endl;
		 cin.get();
		 system ("pause");
		 exit (1);
		}
	cout << "Enter year in the form of yyyy: ";
	cin >> yyyy;//Get year from input
	cin.get();    
   	 
	if(leapyear(year))

		cout << yyyy << " is a leap year.\n";
	else
		cout << yyyy << " is not a leap year\n";

		cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which ";// << endl;
		cout << "it should be: ";// << endl;
		{		
			date.Year = yyyy;
			date.Month = mm;
			date.Day = dd;

			if(dd <= months[mm - 1])
			
				cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
			else
				cout << "Invalid Date for the month!";
			
		 }

cin.get();
return 0;
}

bool leapyear(int year)
  {
	return ((year % 4) == 0);
  }

Hello Ancient Dragon,

so far I got it working without any problem, one thing that I still didn't feel right about this code is that, what if user enter 02-28-2004, it tell yes that is the leapyear, but it didn't correct the date entered, it should display 2004 is the leapyear: February, 29 2004. Another problem is that if user entered 02-29-2004, then it just need to display yyyy is leapyear: February, 29 2004. This code got too complicated, all I want it to do is display the leapyear if divisible by 4 then days range from 1-29, else it is not the leapyear day range from 1-28. Frustrations, I give up:icon_sad:

year = yyyy;
    cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which: ";
    cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
    {
    lp_ck = yyyy%4;
    lp_ck = yyyy%4;
    if(lp_ck==0)
    if(leapyear(year))
      months[1]=29;
      cout << yyyy << " is a leap year.\n";
    else
      months[1]=28;
      cout << yyyy << " is not a leap year\n";
    }

    //if(leapyear(year))
        
        //cout << yyyy << " is a leap year.\n";
    //else
        //cout << yyyy << " is not a leap year\n";

I changed the code in check for leapyear part, it tell me that I error:
[error]
error C2181: illegal else without matching if
[error]

Check your curly braces {}... if you have more than one line of code to run within an if or else , the code must be enclosed in {} . It is good practice to always use these, even if you have a single line of code to run.

Hello Tymk,

Thanks for piont that out, I have spend more times on syntax then code structure. This part of code still not work right, I can not enter and dd > 27 (02-27-08) unless I increase the array size: int months[14]= {31,28,31,30,31,30,31,31,30,31,30,31}; and still did not display February, 29 2004 if it is a leapyear, man I can do this leap year faster in my head then write this program.:confused:

cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which: ";
	//cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
	{
	lp_ck = yyyy%4;
	//if(lp_ck==0)
	if(leapyear(year))
	{
	  months[1]=29;
	  cout << yyyy << " is a leap year.\n";
	}
    else
	{
      months[1]=28;
	  cout << yyyy << " is not a leap year\n";
	}
       }

If you enter 2-28-2008 why do you want to display 2-29-2008 ? All you need to do is validate whether February has 28 or 29 days. If you enter 2-29-2007 then your program should display something to the effect that you entered the wrong number of days.

if( leapyear(year) && month == 2) 
{
    if( dd > months[1]+1 )
       cout << "Invalid # days\n";
}
else
{
    if( dd > months[1] )
       cout << "Invalid # days\n";
}

There is a shorter way to express the above

if( mm == 2)
{
    // if february
    int ndays = (leapyear(year) == true) ? 29 : 28;
    if( dd > ndays)
        cout << "Error\n";
}

Hi Ancient Dragon,

You guys are great, this web site is very useful and valuable to me, your responses are tremendous. I really respect and thank you very much for your help, in that respect, I want to donate some money to keep this site alive for everyone like my self the C++ beginner. I know I have asked many questions this because in20 years I have not expose to C++ or write any programs, I apologize if I asked dump questions that may upset someone. Can you guys tell me how can I make my contribution (donate some money) to this site???:)

Beast Regards,

gazoo

Ancient Dragon ,

Thanks for all your help, I have work on this code too long now, I have all the syntax and it work but not well. Seem like I can't enter number dd =29 for the month of February. The line highlighted it seem not respond to input off dd = 29. I have to inceased the number of an array element from int months[12] to int months[14], please see the completed code below.

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int year);
	
struct // struct declaration
{
	int Month;
	int Day;
	int Year;
	
}date;

int main()
{
		      				
	// Intialize Array For Number Of Days In Each Month
	int months[14]= {31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
	//Initialize array for number of month in the year
	const unsigned int string_month = 12;
	string arr[string_month] = {"January","february","March","April","May","June","July","August","September","October","November","December"}; 
	                                                                          
	int mm, dd, yyyy, year = 0;
	
	cout << "This program will display date and convert number of month to string" << endl;
	//Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
  if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
		 {
         cout << "INVALID month: Please try again.\n" << endl;
		 //cin.get();
		 system ("pause");
		 exit (1);
         }
	cout << "Enter day in the form of dd: ";
	cin >> dd;//Get day from input
  if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid
		 {
         cout << "INVALID day: Please try again.\n" << endl;
		 cin.get();
		 system ("pause");
		 exit (1);
		 }
	cout << "Enter year in the form of yyyy: ";
	cin >> yyyy;//Get year from input
	cin.get();
	year = yyyy;
	cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
		  
if(leapyear(year) && mm == 2 && dd > months[1] + 1)// Check to see if leapyear or not 
	   cout << "Invalid # days for that month\n";
	else if( dd > months[1] )
       cout << "Invalid # days for that month\n";
	else if(leapyear(year)&& mm == 2)  <<< I repeat this line because I can't remove it, if I do it will complain with errors.	  {
	  months[1] = 29;
	  cout << yyyy << " is a leap year.\n";
	  }
    else
	  {
      months[1] = 28;
	  cout << yyyy << " is not a leap year\n";
	  }			
			date.Year = yyyy;
			date.Month = mm;
			date.Day = dd;
			
cin.get();
return 0;
}

bool leapyear(int year)
  {
	return ((year % 4) == 0);
  }

stop using inlinecode and adding your own color tags. It makes it very difficult to copy/paste into my own compiler. Use standard code tags and your program will be color coded.

Hi Ancient Dragon,

. Can you guys tell me how can I make my contribution (donate some money) to this site???:)

Beast Regards,

gazoo

Scroll up to the very top of this page and you will see "DONATE" link on the very top line. You get other benefints for donating too, such as no ads.

I see, thanks for the tips.

stop using inlinecode and adding your own color tags. It makes it very difficult to copy/paste into my own compiler. Use standard code tags and your program will be color coded.

Here is the complete code, it work but not sure, the array I have increased to:
int months[14]= {31,28,31,30,31,30,31,31,30,31,30,31}; enable to cover date 29 for february month. and not sure if the line: if( dd < 1 || dd > months[dd-1] ) work correctely?? and I repeat this line like the one above: else if(leapyear(year)&& mm == 2) if I remove this line it complained.

#include <iostream>
#include <string>
#include <math.h>

using namespace std;

//Declare the function
bool leapyear(int year);
	
struct // struct declaration
{
	int Month;
	int Day;
	int Year;
	
}date;

int main()
{
		      				
	// Intialize Array For Number Of Days In Each Month
	int months[14]= {31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
	//Initialize array for number of month in the year
	const unsigned int string_month = 12;
	string arr[string_month] = {"January","February","March","April","May","June","July","August","September","October","November","December"}; 
	                                                                          
	int mm, dd, yyyy, year = 0;
	
	cout << "This program will display date and convert number of month to string" << endl;
	//Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
  if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
		 {
         cout << "INVALID month: Please try again.\n" << endl;
		 //cin.get();
		 system ("pause");
		 exit (1);
         }
	cout << "Enter day in the form of dd: ";
	cin >> dd;//Get day from input
  if( dd < 1 || dd > months[dd-1] )// Validate to see if input for day is valid for the month
		 {
         cout << "INVALID day: Please try again.\n" << endl;
		 cin.get();
		 system ("pause");
		 exit (1);
		 }
	cout << "Enter year in the form of yyyy: ";
	cin >> yyyy;//Get year from input
	cin.get();
	year = yyyy;
	cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
		  
if(leapyear(year) && mm == 2 && dd > months[1] + 1)// Check to see if leapyear or not 
	   cout << "Invalid # of days for the month of (" << arr[mm - 1] << ") in the year of (" << yyyy << ").\n";
	else if( dd > months[1] )
       cout << "Invalid # of days for the month of (" << arr[mm - 1] << ") in the year of (" << yyyy <<").\n";
	else if(leapyear(year)&& mm == 2)
	  {
	  months[1] = 29;
	  cout << yyyy << " is a leap year.\n";
	  }
    else
	  {
      months[1] = 28;
	  cout << yyyy << " is not a leap year\n";
	  }			
			date.Year = yyyy;
			date.Month = mm;
			date.Day = dd;
			
cin.get();
return 0;
}

bool leapyear(int year)
  {
	return ((year % 4) == 0);
  }:S

hi,

finally you also should implement the correct algorithm to computing leap year:

int LeapYear(int year)
{ return (year % 4 == 0 && year % 100 != 0) || year % 400 == 0 ? 1 : 0; }

brs, cliff

why does months[] have 14 elements but you only use the first 12? Feb 29 has nothing to do with the number of months in a year.

and not sure if the line: if( dd < 1 || dd > months[dd-1] ) work correctely??

That line is incorrect. it should be using mm to index into months[], not dd

if( dd < 1 || dd > months[mm-1] )

Now think about that that is doing. mm is a month number from 1 - 12. But the number of days in January is represented by months[0], not months[1]. So to get to the correct index number on months[] you have to convert mm to be 0-based to that Jan = 0, Feb = 1, ... Dec = 11.

The other part of that equiation -- [icode]dd > months[mm-1][/icode -- is checking if the dd is greater than the number of days in the given month.

Edited 3 Years Ago by Reverend Jim: Fixed formatting

#include <iostream>
#include <string>
#include <cmath>
using namespace std;

//Declare the function
bool leapyear(int year);

struct // struct declaration
{
	int Month;
	int Day;
	int Year;
	
}date;

int main()
{
		      				
	// Intialize Array For Number Of Days In Each Month
	static int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
	//int months[12]= {0,31,28,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
	//Initialize array for number of month in the year
	//const unsigned int string_month = 12;
	static string arr[12]={"January ","February ","March ","April ","May ","June","July ","August ","September ","October ","November ","December "};

	//string arr[string_month] = {"January","February","March","April","May","June","July","August","September","October","November","December"}; 
	                                                                          
	int mm, dd, yyyy, year = 0;
	
	cout << "This program will display date and convert number of month to string" << endl;
	//Prompts user for input
    cout << "Enter month in the form of mm: ";
    cin >> mm;//Get month from input
  if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
		 {
         cout << "INVALID month: Please try again.\n" << endl;
		 //cin.get();
		 system ("pause");
		 exit (1);
         }
	cout << "Enter day in the form of dd: ";
	cin >> dd;//Get day from input
  if( dd < 1 || dd > months[mm-1] )// Validate to see if input for day is valid for the month
		 {
         cout << "INVALID day: Please try again.\n" << endl;
		 cin.get();
		 system ("pause");
		 exit (1);
		 }
	cout << "Enter year in the form of yyyy: ";
	cin >> yyyy;//Get year from input
	cin.get();
	year = yyyy;
	cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
		  
	if(leapyear(year) && mm == 2 && dd > months[2] - 1)// Check to see if leapyear or not 
	   cout << "Invalid # of days for the month of (" << arr[mm - 1] << ").\n";
	else if( dd > months[1] )
       cout << "Invalid # of days for the month of (" << arr[mm - 1] << ").\n";
	if(leapyear(year)&& mm == 2 &&(yyyy / 4) == 0)
	//else if(leapyear(year)&& mm == 2)
	  {
	  months[2] = 28;
	  cout << "The year " << yyyy << " is not a leap year.\n";
	  }
    else
	 {
      months[2] = 29;
	  cout << "The year " << yyyy << " is a leap year\n";
	  }			
			date.Year = yyyy;
			date.Month = mm;
			date.Day = dd;
			
cin.get();
return 0;
}

bool leapyear(int year)
  {
	return ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0);
	//return ((year % 4) == 0);
  }

OK, in that case I have a few options, 1) I can add {0}

static int months[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};

then January become month {1} ...etc but that is not logical. or add a line to convert mm = 1 to {0}, like

if(mm = 1 -> 0)

then I have to this convertion for all 12 months to have it in appropriate place of array elelments. I think this code format is not correct, for indexing the month , should I use the (for) loop some where in the code?? in that case I need to add the ranges of # days for each month(hard coded), there are no easy way aroud it. This program getting complicated just like you have stated in your first message "it can get complicated". When I think a bout it, it does getting complicated. by the way I have send some money to support our website.:icon_smile:
This is the result when I enter 02-29-2004 and 2004 is not a leap year or is it??
[result]
Enter month: 02
Enter date: 29
enter year:2004
you have enter 2-29-2004 which Invalid # of days for the month of ( february).
The year 2004 is a leap year.
[/result]
2004 is not a leap year if I'm not misstaken but 2001 is, nor 2 - 28 - 2001 since it has the wrong date. :icon_confused:

All the numbers starting from 0 and divisible by four are leap years.

if(year%4==0)
{
cout<<"THIS IS A LEAP YEAR" <<year;
}

Am i not correct?
(Ps: the above code is not related to your program. Sorry.!!)

omg sky diploma,

your leap-year rule is from the Julian calendar, which was superseded anno domini 1581 by Pope Gregor's new Gregorian calendar. View #22 where this very new rule is given !

krs, cilff

OK Sky Diploma,

You are correct, sorry about the confusion, seem like this leap year assignment is very popular. Who has invented the leap year must notice that earth rotation is got slower in that particular year, it take an extra 24 hrs to completed the year. You guys are awesome, thanks for all your help.

Regards,

KMH

All the numbers starting from 0 and divisible by four are leap years.

if(year%4==0)
{
cout<<"THIS IS A LEAP YEAR" <<year;
}

Am i not correct?
(Ps: the above code is not related to your program. Sorry.!!)

If I'm mistaken then there something wrong with my code, it tell me that 2004 or 2001 is not the leap year, I should stop working on this code, and if I continue working on it I will turn to roasted nut.

You are mistaken. It was a leap year.

if( year % 4 == 0) Most of the time if that is true then year is a leap year. 2001 is NOT a leap year because it is not evenly divisible by 4. 2004 IS a leap year because it is evenly divible by 4.

A year will be a leap year if it is divisible by 4 but not by 100. If a year is divisible by 4 and by 100, it is not a leap year unless it is also divisible by 400.

Thus years such as 1996, 1992, 1988 and so on are leap years because they are divisible by 4 but not by 100. For century years, the 400 rule is important. Thus, century years 1900, 1800 and 1700 while all still divisible by 4 are also exactly divisible by 100. As they are not further divisible by 400, they are not leap years.

http://www.dataip.co.uk/Reference/LeapYear.php

This article has been dead for over six months. Start a new discussion instead.