/**
* birthdays.cpp
*
* <#Name#>changyu liu
* <#Uniqname#>lchangyu
*
* EECS 183: Project 2
*
* <#description#>
*/
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
/**
* Requires: nothing
* Modifies: cout
* Effects: prints out the initial heading for the program
*/
void printHeading();
/**
* Requires: nothing
* Modifies: cout
* Effects: prints out the final greeting for the program
*/
void printCloser();
/**
* Requires: nothing
* Modifies: cout
* Effects: prints the menu
*/
void printMenu();
/**
* Note: write your test cases in main() BEFORE you implement this function
*
* Requires: nothing
* Modifies: cout, cin
* Effects: prints the menu
* reads the input from the user
* checks to make sure the input is within range for the menu
* If not prints "Invalid menu choice"
* continues to print the menu and read an input until a valid one is
* entered
* returns the users choice of menu options
*/
int getMenuChoice();
/**
* Note: write your test cases in main() BEFORE you implement this function
*
* Requires: month, day, year may represent a date
* Modifies: nothing
* Effects: returns 'true' if the date is in the limits
* of the Gregorian calendar otherwise returns 'false'
*/
bool isGregorianDate(int month, int day, int year);
/**
* Note: write your test cases in main() BEFORE you implement this function
*
* Requires: year is a Gregorian year
* Modifies: nothing
* Effects: Returns 'true' if the year is a leap year
* otherwise returns 'false'
*/
bool isLeapYear(int year);
/**
* Note: write your test cases in main() BEFORE you implement this function
*
* Requires: month, day, year may represent a date
* Modifies: nothing
* Effects: Returns 'true' if the date is valid
* otherwise returns 'false'
* see the spec for definition of "valid"
*/
bool isValidDate(int month, int day, int year);
/**
* Note: write your test cases in main() BEFORE you implement this function
*
* Requires: month, day, year is a valid date
* i.e., the date passed to this function has already passed
* isValidDate()
* Modifies: nothing
* Effects: Returns the value that Zeller's formula calculates
*/
int determineDay(int month, int day, int year);
/**
* Note: write your test cases in main() BEFORE you implement this function
*
* Requires: day (0 represents Saturday, 1 Sunday, 2 Monday, 3 Tuesday, etc)
* Modifies: cout
* Effects: prints the day you were born on
* Sunday, Monday, ..., Saturday
*/
void printDayOfBirth(int day);
/**
* Note: write your test cases in main() BEFORE you implement this function
*
* Requires: nothing
* Modifies: cout, cin
* Effects: Asks for the Month/day/year of their birth
* If the date is valid, it will print the day
* of the week you were born on
* Otherwise, it will print "Invalid date" prompt
*/
void determineDayOfBirth();
/**
* Note: write your test cases in main() BEFORE you implement this function
*
* Base Project
* Requires: nothing
* Modifies: cout
* Effects: prints "Under Construction"
*
* S'more version of this function
* Requires: nothing
* Modifies: cout, cin
* Effects: reads the month and day of birthday
* loops through 10 years printing the day of the week
* the birthday falls on
* If the month/day is not valid, it prints nothing
*/
void print10Birthdays();
/**
* Note: write your test cases in main() BEFORE you implement this function
*
* Requires: nothing
* Modifies: nothing
* Effects: Asks for a Gregorian year and prints the
* 10 leap years after (not including) the year entered.
* If the year is invalid, it prints nothing.
*/
void print10LeapYears();
int main() {
int month;
int day;
int year;
char placeHolder1;//holds "/"
char placeHolder2;//holds 2nd "/"
printHeading();//prints the heading in int
cout << "Enter your date of birth -->" << endl << endl;
cin >> month >> placeHolder1 >> day >> placeHolder2 >> year;
if (!isValidDate(month, day, year)) {
//if the function returns true, this will output "invalid"
cout << "Invalid date";
}
else {//if the date passes through the two previous functions, the date is valid and will be evaluated
day = determineDay(month, day, year);
cout << endl << endl << "Have a great birthday!!!";
}
return 0;
}
void printHeading() {
cout << "*******************************" << endl
<< " Birthday Calculator " << endl
<< "*******************************" << endl << endl;
}
void printCloser() {
cout << endl;
cout << "****************************************************" << endl
<< " Thanks for using the Birthday Calculator " << endl
<< "****************************************************" << endl
<< endl;
}
void printMenu() {
cout << endl << endl;
cout << "Menu Options" << endl
<< "------------" << endl;
cout << "1) Determine day of birth" << endl;
cout << "2) Print the next 10 leap years" << endl;
cout << "3) Determine birthdays for the next 10 years" << endl;
cout << "4) Finished" << endl << endl;
cout << "Choice --> ";
}
int getMenuChoice() {
int userChoice = 0;
string clearInputStream;
printMenu();
cin >> userChoice;
if (userChoice != 1 && userChoice != 2 && userChoice != 3 && userChoice !=4)
{
while (userChoice != 1 && userChoice != 2 && userChoice != 3 && userChoice !=4) {
if (!cin) {
cin.clear();
getline (cin, clearInputStream);
}
cout << "Invalid menu choice" << endl;
printMenu();
cin >> userChoice;
}
}
return userChoice;
return 0;
}
bool isGregorianDate(int month, int day, int year) {
if (year > 1752) {
return true;
}
else if (year == 1752) {
if (month < 9) {
return false;
}
else if (month > 9) {
return true;
}
else if (month == 9) {
if (day < 14) {
return false;
}
else return true;
}
}
return false;
}
bool isLeapYear(int year) {
if ((year % 4 == 0 && year % 100 != 0) || (year % 400 == 0)) {
return true;
}
else{
return false;
}
}
bool isValidDate(int month, int day, int year) {
if (!isGregorianDate(month, day, year)) {
return false;
}
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
if (day >= 1 && day <32) {
return true;
}
else{
return false;
}
break;
case 4:
case 6:
case 9:
case 11:
if (day >= 1 && day < 31) {
return true;
}
else{
return false;
}
break;
case 2:
if (isLeapYear(year)) {
if (day >= 1 && day <30) {
return true;
}
}
else if (day >= 1 && day < 29) {
return true;
}
else{
return false;
}
break;
default:
return false;
break;
}
}
int determineDay(int month, int day, int year) {
int zday = day;
int zmonth;
int zyear;
int zyear1;
int zcentury;
if (month == 1 || month == 2) {
zmonth = month + 12;
zyear = year - 1;
}
else {
zmonth = month;
zyear = year;
}
zyear1 = zyear % 100;
zcentury = zyear / 100;
day = (zday + (static_cast<int>((13 * (zmonth + 1)) / 5.0)) + zyear1
+ (zyear1 / 4) + (zcentury / 4) + 5 * zcentury) % 7;
return day;
}
void printDayOfBirth(int day) {
switch (day) {
case 0:
cout << "You were born on a: Saturday";
break;
case 1:
cout << "You were born on a: Sunday";
break;
case 2:
cout << "You were born on a: Monday";
break;
case 3:
cout << "You were born on a: Tuesday";
break;
case 4:
cout << "You were born on a: Wednesday";
break;
case 5:
cout << "You were born on a: Thursday";
break;
case 6:
cout << "You were born on a: Friday";
default:
break;
}
}
void determineDayOfBirth() {
int month;
int day;
int year;
char forwardslash; // reads character between month and day
char forwardslash2; // reads character between day and year
cout << "Enter your date of birth " << endl << "format: month / day / year -->" << endl;
cin >> month >> forwardslash >> day >> forwardslash2 >> year;
if (!isValidDate(month, day, year)) {
cout << "Invalid date";
}
else {
day = determineDay(month, day, year);
printDayOfBirth(day); }
cout << endl << endl << "Have a great birthday!!!" << endl;
}
void print10Birthdays() {
cout << "Under Construction" << endl;
return;
}
void print10LeapYears() {
int count=1,i;
cout << "Enter the Gregorian year :" <<endl;
cin >> i;
cout << "Next 10 leap years are :" <<endl;
while (count <11){
i = i + 1;
if (isLeapYear(i)) {
cout << i <<endl;
count++;
}
}
return;
}