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;
}