1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
|
// Calendar
/* This program stores a calendar month of dates (numbers starting at 1) in a two-dimensional array.
Some months have 31 days, others 30, and February has 28 (29 if it’s a leap year, which is a year divisible by 4). */
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#define TRUE 1
#define FALSE 0
const int SIZE = 12;
const int col = 7;
int days_in_month[SIZE]={31,0,31,30,31,30,31,31,30,31,30,31};
const int rows = 5;
int getDaysInMonth(string, string [], int []);
void determineLeapYear(int);
void showCal(string, int, int [][col], int, int [], string [], string);
void validateDay(string, string []);
void validateMonth(string, string []);
int month[rows][col] =
{
{1, 2, 3, 4, 5, 6, 7},
{8, 9, 10, 11, 12, 13, 14},
{15, 16, 17, 18, 19, 20, 21},
{22, 23, 24, 25, 26, 27, 28},
{29, 30, 31, 0, 0, 0, 0}
};
string dayOfWeek[7] = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
string monthName[SIZE] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};
int year[4] = {2016, 2017, 2018, 2019};
int dayNumber[7] = {1, 2, 3, 4, 5, 6, 7};
int main()
{
string choice;
string GET_DAY;
int GET_YEAR;
cout << "Enter the name of a Month> ";
getline(cin, choice);
validateMonth(choice, monthName);
while (choice != "Stop" && choice != "stop")
{
cout << "\nEnter the year> ";
cin >> GET_YEAR;
cout << endl;
determineLeapYear(GET_YEAR);
cout << "\nEnter the day the month starts> ";
cin >> GET_DAY;
validateDay(GET_DAY, dayOfWeek);
cout << "\n";
showCal(choice, GET_YEAR, month, rows, dayNumber, monthName, GET_DAY);
cout << "\nEnter the name of a month or enter stop to quit>";
cin.ignore();
getline(cin, choice);
validateMonth(choice, monthName);
}
system("pause");
return 0;
}
//*************************************************************************************
// Definition of the function validateMonth which test the input of the user for GET_DAY*
//*************************************************************************************
void validateMonth(string choice, string monthName []){
int found = 0;
int a = 0;
for (a = 0; a < 7 && !found; a++)
{
if (monthName[a] == choice || choice == "stop" || choice == "Stop")
{
found = 1;
}
} // end for loop
if (found)
{
cout << endl;
}
else
{
cout << "ERROR: The day " << choice << " was not found...please again!" << endl << endl;
}
}
//*************************************************************************************
// Definition of the function validateDay which test the input of the user for GET_DAY*
//*************************************************************************************
void validateDay(string GET_DAY, string dayOfWeek []){
int found = 0;
int a = 0;
for (a = 0; a < 7 && !found; a++)
{
if (dayOfWeek[a] == GET_DAY)
{
found = 1;
}
} // end for loop
if (found)
{
cout << endl;
}
else
{
cout << "ERROR: The day " << GET_DAY << " was not found...please again!" << endl << endl;
}
}
//***********************************************************************************
// Definition of the function determineLeapYear which determines if it is leap year *
// or not in order to provide the right number of days for February. *
//***********************************************************************************
void determineLeapYear(int GET_YEAR)
{
if(GET_YEAR% 4 == FALSE && GET_YEAR%100 != FALSE ||GET_YEAR%400 == FALSE)
{
days_in_month[1] = 29;
}
else
{
days_in_month[1] = 28;
}
}
//***********************************************************************************
// Definition of the function showCal which formats the calendar of each month *
// depending on which day the month starts on. *
//***********************************************************************************
void showCal(string choice, int GET_YEAR, int month[][col], int rows, int dayNumber[], string monthName [], string GET_DAY)
{
int days;
int numDays = 0;
int start_day = 0;
cout << "\t" << choice << "\t" << GET_YEAR << endl;
for (int count = 0; count < SIZE; count++)
{
if (choice == monthName[count])
{
numDays += days_in_month[count];
}
}
for (int c = 0; c < 7; c++)
{
if (GET_DAY == dayOfWeek[c])
{start_day += dayNumber[c];}
}
//this loop sets the position for the starting day
for (int i = 0; i < start_day; i++)
{
cout << "\t";
}
//This loop displays the days
for (month[rows][col] = (1+start_day); month[rows][col] <= (numDays+start_day); month[rows][col]++)
{
cout << (month[rows][col]-start_day) << "\t";
if( month[rows][col]%7 == 0){
cout << endl;
}
}
cout << endl;
}
|