/* This program will get the day the month starts on, and the day it ends on, then it will output a calendar. Made by Nathan Anderson 4-5-01 Exercise 11 ---------------------Algorithm------------------- set BoxWidth as 8, BoxHeight as 3 Get the starting day error check set 'day' to StartDay * -1 + 2 Get amount of days in month calculate amount of weeks (EndDay + StartDay - 2) / 7 Show days, Sunday through Saturday output '*' (BoxWidth * 7 + 1) times do the following 'weeks' times output '*' do the following 7 times if day is smaller than 1, or bigger than 'EndDay', output space else output day add 1 to day value end line do the following 'BoxHeight' + 1 times ouput '*' do the following 7 times set width to 'BoxWidth' output '*' output '*' (BoxWidth * 7 + 1) times */ //-------------------Includes-------------------- #include //--------------Function Prototypes-------------- int ShowDateLine(int day, int EndDay, int BoxWidth); void ShowRestOfBox(int BoxWidth, int BoxHeight); void ShowDates(int BoxWidth); void Divider(int BoxWidth); //---------------------Main---------------------- int main() { int StartDay, EndDay, day, counter, weeks, end; const int BoxWidth = 8, BoxHeight = 3; cout << "1. Sun 2. Mon 3. Tue 4. Wed " << endl; cout << "5. Thu 6. Fri 7. Sat" << endl << endl; do { cout << "Enter the starting day: "; cin >> StartDay; if ((StartDay < 1) || (StartDay > 7)) { cout << "\aError, must be between 1 and 7.\n"; } } while ((StartDay < 1) || (StartDay > 7)); //Error checking day = StartDay * -1 + 2; cout << "Enter the amount of days in the month: "; cin >> EndDay; cout << endl; weeks = (EndDay + StartDay - 2) / 7; weeks++; //----------------Draw Calendar------------------ ShowDates(BoxWidth); Divider(BoxWidth); for (counter = 0; counter < weeks; counter++) { day = ShowDateLine(day, EndDay, BoxWidth); ShowRestOfBox(BoxWidth, BoxHeight); Divider(BoxWidth); } cin >> end; // Used to keep window open } //------------------Functions-------------------- int ShowDateLine(int day, int EndDay, int BoxWidth) /* Outputs a '*' then it outputs the days till it reaches the 'EndWeek' value. If day reaches the EndDay value, then it will output a space instead of a day. After each date it will output a '*', then proceed 'BoxWidth' spaces ahead. */ { int counter, EndWeek; cout.setf(ios::right); cout << "*"; EndWeek = day + 7; for (counter = 0; counter < 7; counter++, day++) { cout.width(BoxWidth - 1); if ((day < 1) || (day > EndDay)) { cout << " "; } else { cout << day; } cout << "*"; } cout << endl; return day; } //----------------------------------------------- void ShowRestOfBox(int BoxWidth, int BoxHeight) /* Outputs a '*' every 'BoxWidth' length, for a total of 8 '*'. It does that 'BoxHeight' times. */ { int countera, counterb; cout.setf(ios::right); for (countera = 1; countera < BoxHeight + 0; countera++) { cout << "*"; for (counterb = 0; counterb < 7; counterb++) { cout.width(BoxWidth); cout << "*"; } cout << endl; } } //----------------------------------------------- void ShowDates(int BoxWidth) /* Outputs the all 7 days of the week. It takes the 'BoxWidth' and outputs the day in the middle of where the box will be. */ { cout.setf(ios::right); cout.width(BoxWidth / 2 + 2); cout << "Sun"; cout.width(BoxWidth); cout << "Mon"; cout.width(BoxWidth); cout << "Tue"; cout.width(BoxWidth); cout << "Wed"; cout.width(BoxWidth); cout << "Thu"; cout.width(BoxWidth); cout << "Fri"; cout.width(BoxWidth); cout << "Sat" << endl; } //----------------------------------------------- void Divider(int BoxWidth) //Outputs a bar of '*' 'BoxWidth' * 7 + 1 { int length, counter; length = BoxWidth * 7 + 1; for (counter = 0; counter < length; counter++) { cout << "*"; } cout << endl; } /*--------------------Output--------------------- 1. Sun 2. Mon 3. Tue 4. Wed 5. Thu 6. Fri 7. Sat Enter the starting day: 2 Enter the amount of days in the month: 28 Sun Mon Tue Wed Thu Fri Sat ********************************************************* * * 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* * * * * * * * * * * * * * * * * * * * * * * ********************************************************* */