help with program that accepts a date in the format MM-DD-YYYY from the user and determines and displays the day number 1-366 and has leap year bool variable. I've been trying to fo figure this out but i cant
output should be something like this although my calculations are wrong i think?
*Enter a date [MM-DD-YYYY]: 01-03-1985
Error invalid*
But also i need to fit in the error valid in case theres an error and i dont know how i would exactly put that in. Can anyone help?
When i do it, it outputs everything along with the error value and its supposed to output only the entered date and if its invalid then it would output the month name and year and day of the year, but if invalid it would just output that its invalid
*******************************************************
#include <iostream>
using namespace std;
bool isLeapYear(int year) //bOOL VARIABLE where calculations to determine whetherits a leap year or not are done
{
bool leapYear;
// (!(year % 100 && year % 4 || year % 400));
if ((year % 4 == 0) && (year % 100 != 0)) //calculations on every 4 years since its leap year with module to get remainder, if remiander then it is not
{
//leap year
return leapYear = true;
}
else //if else statement to get true or false based on the year given
{
return leapYear = false;
}
}
//**************************************
int daysOfYear(int month, int years, int days)
{
string monthName;
int dayYear;
if(isLeapYear(years)) //to determine whether febraury has 28 or 29 days based on calling bool function from earlier
{
dayYear = 366;
switch(month) //switch stament gives us the amount of days ina year based on input given
{ //case number is based on month, and days based on the days inputed to get 'dayYear' which is amount of days in a year
case 1: //Month January
monthName = "January";
dayYear -= (dayYear - days);
break;
case 2: //Month February
monthName = "February";
if(isLeapYear(years)) //to determine whether febraury has 28 or 29 days based on calling bool function from earlier
{
dayYear -= (335 - days);
case 3: //Month March
monthName = "March";
dayYear -= (306 - days);
break;
case 4: //Month April
monthName = "April";
dayYear -= (275- days);
break;
case 5: //month May
monthName = "May";
dayYear -= (245 - days);
break;
case 6:
monthName = "June";
dayYear -= (214 - days); //month june
break;
case 7:
monthName = "July";
dayYear -= (183 - days);
break;
case 8: //Month August
monthName = "August";
dayYear -= (153 - days);
break;
case 9: //September
monthName = "September";
dayYear -= (122 - days);
break;
case 10: //Month of october
monthName = "October";
dayYear -= (92 - days);
break;
case 11: //Month of November
monthName = "November";
dayYear -= (61 - days);
break;
case 12: //Month of December
monthName = "December";
dayYear -= (31 - days);
break;
return dayYear;
}
cout << monthName << " " << days << ", " << years << " is day number " << dayYear << endl;
}
//*************************************Function #3 ****************************************************************************
//This function determines whether months, days, years is invalid and outputs it, whatever is given that is invalid
void invalidDate(int month, int days, int years)
{
if (month > 12)
{
cout << "Error, invalid month " << month << " entered.\n";
cout << "Please run the program again with a valid date.\n";
}
if (days < 1 || days > 31)
{
cout << "Error, invalid day " << days << " entered.\n";
cout << "Please run the program again with a valid date.\n";
}
if (years > 9999 || years < -0)
{
cout << "Error, invalid year " << years << " entered.\n";
cout << "Please run the program again with a valid date.\n";
}
}
int main()
{
int month, days, years;
cin >> month >> days >> years;
cout << "Enter a date [MM-DD-YYYY]: " << month << "-" << days << "-" << years << endl;
isLeapYear(years);
daysOfYear(month, years, days);
invalidDate(month, days, years);
bool is_leap( int year )
{
if( year%400 == 0 ) returntrue ;
elseif( year%100 == 0 ) returnfalse ;
elsereturn year%4 == 0 ;
}
int days_in_month( int month, int year ) // jan == 1
{
switch(month)
{
case 1 : case 3 : case 5 : case 7 : case 8 : case 10 : case 12 : return 31 ;
case 4 : case 6 : case 9 : case 11 : return 30 ;
case 2 : return 28 + is_leap(year) ; // 28 or 29 if leap year
default: return 0 ; // sanity check
}
}
bool is_valid_date( int mm, int dd, int yy )
{
return yy > 1599 && // valid year
mm > 0 && mm < 12 && // valid month
dd > 0 && dd <= days_in_month( mm, yy ) ; // valid day
}
int day_number( int mm, int dd, int yy )
{
if( !is_valid_date(mm,dd,yy) ) return 0 ; // get rid of trash
int ndays = 0 ;
// sum up all the days in months up to the previous (last completed) month
// for( int month = 1 ; month < mm ; ++m ) ndays += days_in_month( monthy, yy ) ;
for( int month = 1 ; month < mm ; ++month ) ndays += days_in_month( month, yy ) ;
return ndays + dd ; // add the days in the current month
}
Test and verify that these are working correctly.
Then, assemble them to get your program.
Test and verify that the program is working correctly.
I think in your haste, you messed up line 32 a little bit.
Shouldn't for( int month = 1 ; month < mm ; ++m ) ndays += days_in_month( monthy, yy ) ;
be for( int month = 1 ; month < mm ; ++month ) ndays += days_in_month( month, yy ) ; ??