leap year program

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 35
Reputation: gazoo is an unknown quantity at this point 
Solved Threads: 0
gazoo gazoo is offline Offline
Light Poster

leap year program

 
0
  #1
Apr 12th, 2008
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.
  1.  
  2. #include <iostream>
  3. #include <string>
  4. #include <math.h>
  5.  
  6. using namespace std;
  7.  
  8. //Declare the function
  9. bool leapyear(int lpyr);
  10.  
  11. struct // struct declaration
  12. {
  13. int Month;
  14. int Day;
  15. int Year;
  16.  
  17. }date;
  18.  
  19. int main()
  20. {
  21.  
  22. // Intialize Array For Number Of Days In Each Month
  23. int months[12]= {31,29,31,30,31,30,31,31,30,31,30,31}; //Array For Number Of Days In Each Month
  24. //Initialize array for number of month in the year
  25. const unsigned int string_month = 12;
  26. string arr[string_month] = {"January","february","March","April","May","June","July","August","September","October","November","December"};
  27.  
  28. int mm, dd, yyyy, lpyr = 0;
  29.  
  30. cout << "This program will display date and convert number of month to string" << endl;
  31. //Prompts user for input
  32. cout << "Enter month in the form of mm: ";
  33. cin >> mm;//Get month from input
  34. cout << "Enter day in the form of dd: ";
  35. cin >> dd;//Get day from input
  36. cout << "Enter year in the form of yyyy: ";
  37. cin >> yyyy;//Get year from input
  38. cin.get();
  39.  
  40. if( mm < 1 || mm > 12 )// Validate to see if input for month is valid
  41. {
  42. cout << "INVALID month: Please try again." << endl;
  43. cin.get();
  44. exit (1);
  45. }
  46.  
  47. else if( dd < 1 || dd > 31 )// Validate to see if input for day is valid
  48. {
  49. cout << "INVALID day: Please try again." << endl;
  50. cin.get();
  51. exit (1);
  52. }
  53. else
  54. cout << "You have entered: " << mm << "-" << dd << "-" << yyyy << " which " << endl;
  55. /*{
  56.   [COLOR="Green"]if(lpyr % 4 != 0)
  57.   return false;
  58.   else if(lpyr % 100 != 0)
  59.   return true;
  60.   else if(lpyr % 400 != 0)
  61.   return false;
  62.   else
  63.   return true;[/COLOR] }*/
  64.  
  65. dd = dd + 1;// Increase the number of days in feb to 29
  66. cout << yyyy << " is a leap year and it is should be." << endl;
  67.  
  68. {
  69. date.Year = yyyy;
  70. date.Month = mm;
  71. date.Day = dd;
  72.  
  73. if(dd <= months[mm -1])
  74.  
  75. cout << arr[mm - 1] << ", " << date.Day << " " << date.Year << endl;
  76. else
  77. cout << "Invalid Date for the month!";
  78. }
  79.  
  80. cin.get();
  81. return 0;
  82. }
Last edited by Ancient Dragon; Apr 12th, 2008 at 9:22 am. Reason: corrected code tags and add line numbers
Reply With Quote Quick reply to this message  
Join Date: Mar 2008
Posts: 672
Reputation: Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold Sky Diploma is a splendid one to behold 
Solved Threads: 99
Sky Diploma's Avatar
Sky Diploma Sky Diploma is offline Offline
Practically a Master Poster

Re: leap year program

 
0
  #2
Apr 12th, 2008
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.
1. Please Mark Your Thread as Solved After Getting Your Answers.
2. Please Use CODE TAGS .
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: leap year program

 
0
  #3
Apr 12th, 2008
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 35
Reputation: gazoo is an unknown quantity at this point 
Solved Threads: 0
gazoo gazoo is offline Offline
Light Poster

Re: leap year program

 
0
  #4
Apr 12th, 2008
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

Originally Posted by Ancient Dragon View Post
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.
Last edited by gazoo; Apr 12th, 2008 at 10:04 pm.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: leap year program

 
0
  #5
Apr 12th, 2008
First, the code you posted in red isn't needed.
  1. if( leapyear(year) )
  2. cout << "is a leap year\n";
  3. else
  4. cout << "is not a leap year\n";
  5.  
  6. bool leapyear( int year)
  7. {
  8. // its actually a little more complicated than this but will do for
  9. // years in the 20th and 21st centuries.
  10. return (year % 4) == 0 ? true : false;
  11. }
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 35
Reputation: gazoo is an unknown quantity at this point 
Solved Threads: 0
gazoo gazoo is offline Offline
Light Poster

Re: leap year program

 
0
  #6
Apr 12th, 2008
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>'
Originally Posted by Ancient Dragon View Post
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;}
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: leap year program

 
0
  #7
Apr 13th, 2008
you double posted the code.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 35
Reputation: gazoo is an unknown quantity at this point 
Solved Threads: 0
gazoo gazoo is offline Offline
Light Poster

Re: leap year program

 
0
  #8
Apr 13th, 2008
Sorry what is that mean??

Originally Posted by Ancient Dragon View Post
you double posted the code.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1464
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Still Learning

Re: leap year program

 
0
  #9
Apr 13th, 2008
Originally Posted by gazoo View Post
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.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 35
Reputation: gazoo is an unknown quantity at this point 
Solved Threads: 0
gazoo gazoo is offline Offline
Light Poster

Re: leap year program

 
0
  #10
Apr 13th, 2008
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;
}
Originally Posted by Ancient Dragon View Post
Re-read the code you posted -- its there twice. Repost and this time preview your post before hitting the Submit button.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC