JDL Date program, C++

This is an assignment i’ve been working on for school.

The basic premise is that the program should return the day of the week of any date throughout all history.

Basically, I need to check if the date the user inputs can be checked to find out if their a part of the gregorian calander or not.

I did this using a nested if, and the function is listed in the correct place in the program.

However, it isn’t checking for if the date is gregorian or not despite my nested if.

Any help/ideas would be appreciated.

thanks.

‘’’

// JDNDayOfWeek.cpp : This file contains the ‘main’ function. Program execution begins and ends there.
//
// This program allows the user to enter a date and outputs the day of the week for that date.
//
// To complete this project, update the code in the isDateValid and isDateFromGregorianCalendar functions at the end of the file.
// The Gregorian Calendar started 10/15/1582 so if the date is on or after 10/15/1582, the function should return true.
//
// The prototypes and function calls have already been added.
// Temporarily, both functions have been set up to always return true.
//
//
// Note that there is an enumerated type Days defined just after using namespace std;
// That just means SUN = 0, MON = 1, TUE = 2, etc.
// They are in caps because those values cannot be changed.
// They are used for comparison (in the switch statement just before the output).
//

#include
#include

using namespace std;

enum Days { SUN, MON, TUE, WED, THU, FRI, SAT };

// prototypes
bool isDateValid(int monthIn, int dayIn, int yearIn);
bool isDateFromGregorianCalendar(int monthIn, int dayIn, int yearIn);

int main()
{
// declare variables
int month;
int day;
int year;
int intRes1;
int intRes2;
int intRes3;
long JDN;
bool bValidDate;
bool bFromGregCal = true;
int dayOfWeek;
string dayStr;
int userMonth;
int userDay;
int userYear;

// input date
cout << "Enter a date in the form mm/dd/yyyy (0 to quit): ";
cin >> month;

while (month != 0)
{
	cin.get();
	cin >> day;
	cin.get();
	cin >> year;

	userMonth = month;
	userDay = day;
	userYear = year;

	
	// validate date
	bValidDate = isDateValid( month, day, year);

	if (bValidDate)
	{


		// check if date is from Gregorian calendar - on or after 10/15/1582
		bFromGregCal = isDateFromGregorianCalendar(month, day, year);

		// calculate JDN
		if (month == 1 || month == 2)
		{
			month += 12;
			year--;
		}

		// if date comes from Gregorian calendar - on or after 10/15/1582 
		if (bFromGregCal)
		{
			intRes1 = 2 - year / 100 + year / 400;
		}
		else
		{
			intRes1 = 0;
		}
		intRes2 = static_cast<int>(365.25 * year);
		intRes3 = static_cast<int>(30.6001 * (month + 1));
		JDN = intRes1 + intRes2 + intRes3 + day + 1720994.5;

		dayOfWeek = (JDN + 2) % 7;


		// output
		switch (dayOfWeek)
		{
		case SUN:
			dayStr = "Sunday";
			break;
		case MON:
			dayStr = "Monday";
			break;
		case TUE:
			dayStr = "Tuesday";
			break;
		case WED:
			dayStr = "Wednesday";
			break;
		case THU:
			dayStr = "Thursday";
			break;
		case FRI:
			dayStr = "Friday";
			break;
		default:				// SAT
			dayStr = "Saturday";
		}


		cout << endl;
		cout << userMonth << "/" << userDay << "/" << userYear
			<< " is a " << dayStr << ".";
		cout << endl << endl;
	}
	else
	{
		cout << endl;
		cout << userMonth << "/" << userDay << "/" << userYear
			<< " is not a valid date.";
		cout << endl << endl;
	}


	// input next date
	cout << "Enter a date in the form mm/dd/yyyy (0 to quit): ";
	cin >> month;

}	// while (month != 0)

cout << endl << endl;
return 0;

}

// isDate Valid function - start with most common information
// monthIn should be 1 - 12
// dayIn should be 1 - 31
// yearIn should be a positive number
//
// Once that works, check that dayIn is 1-31 for Jan March, May, July, Aug, Oct and Dec.
// dayIn should be 1-28 for Feb and 1-30 for other months
//
// Once that works, check for leap year
//
// Please do NOT start by trying to check for all those things right off the bat.
// Make small changes and save your work.

bool isDateValid(int monthIn, int dayIn, int yearIn)
{

bool bValid = true;

// add code to validate the dateIn

if (monthIn = 1, 3, 5, 7, 8, 10, 12 && dayIn > 1 && dayIn < 31)
{
	return bValid;
}

else if (monthIn = 2 && dayIn > 1 && dayIn < 28)
{
	return bValid;
}

else if (monthIn = 4, 6, 9, 11 && dayIn > 1 && dayIn < 30)
{
	return bValid;
}

else
{
	return bValid == false;
}

}

// There are many ways to approach this: nested ifs or using compound if statements.
// To me, the most direct way is to start by checking the year, then if the the year = 1582 and check the month,
// then if the year = 1582 and month = 10, check the day.
// I have seen this written a number of ways so do not feel like you have to do it the way I just described.
// You should write the code how it makes the most sense to you…and of course, it should work! :slight_smile:

// check if date is from Gregorian calendar - on or after 10/15/1582
bool isDateFromGregorianCalendar(int monthIn, int dayIn, int yearIn)

{

	// add code to check if date is on or after 10/15/1582

	if ( yearIn >= 1582)
	{
		if (monthIn >= 10)
		{
			if (dayIn >= 15)
			{
				return true;
			}
		}
	}

	else
	{

		return false;

	}
}

‘’’

I can look tomorrow, but in the mean time, it’d help if you add the symbols ``` on new lines just before and after your block of code in your post.

Your formatting is still off. Hopefully a mod will be around to help us fix it.

Here is your code:

// add code to check if date is on or after 10/15/1582
if (yearIn >= 1582) {
  if (monthIn >= 10) {
    if (dayIn >= 15) {
      return true;
    }
  }
} else {
  return false;
}

Here I’ve isolated just the if statements that you were asking about. Lets think about some sample cases.

  1. 02/12/1581 - false, just like we want
  2. 11/16/1582 - true, just like we want
  3. 12/16/1582 - true, good…
  4. 01/16/1583 - false, uh oh… What happened here?