#include "stdafx.h" #include #include #include using namespace std; void doExercise1Solution() { // Declare 3 variables, for a hard-coded date. int day = 3, month = 12, year = 2011; // Is the date a leap year? bool isLeapYear = ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0); cout << "Is " << year << " a leap year? " << isLeapYear << endl; } void doExercise2Solution() { // Declare 3 variables, for a hard-coded date. int day = 3, month = 12, year = 2011; bool isLeapYear = ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0); // Is the date valid? bool isValid; if (month < 1 || month > 12) { isValid = false; } else if (year < 0 || year > 2099) { isValid = false; } else if (day < 1) { isValid = false; } else { int maxDay; switch (month) { case 2: maxDay = (isLeapYear) ? 29 : 28; break; case 4: case 6: case 9: case 11: maxDay = 30; break; default: maxDay = 31; break; } isValid = (day <= maxDay); } cout << day << "/" << month << "/" << year << " is valid? " << isValid << endl; } void doExercise3Solution() { int day, month, year; string response; do { // Get a date from the user. cout << "Day: "; cin >> day; cout << "Month: "; cin >> month; cout << "Year: "; cin >> year; bool isLeapYear = ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0); bool isValid; if (month < 1 || month > 12) { isValid = false; } else if (year < 0 || year > 2099) { isValid = false; } else if (day < 1) { isValid = false; } else { int maxDay; switch (month) { case 2: maxDay = (isLeapYear) ? 29 : 28; break; case 4: case 6: case 9: case 11: maxDay = 30; break; default: maxDay = 31; break; } isValid = (day <= maxDay); } cout << day << "/" << month << "/" << year << " is valid? " << isValid << endl; // Ask the user whether he/she wants to enter another date. cout << endl << "Enter another date? "; cin >> response; } while (response == "yes"); cout << "The End." << endl; } void doExercise4Solution() { int day, month, year; string response; string allValidDates; do { // Get a date from the user. cout << "Day: "; cin >> day; cout << "Month: "; cin >> month; cout << "Year: "; cin >> year; bool isLeapYear = ((year % 4 == 0) && !(year % 100 == 0)) || (year % 400 == 0); bool isValid; if (month < 1 || month > 12) { isValid = false; } else if (year < 0 || year > 2099) { isValid = false; } else if (day < 1) { isValid = false; } else { int maxDay; switch (month) { case 2: maxDay = (isLeapYear) ? 29 : 28; break; case 4: case 6: case 9: case 11: maxDay = 30; break; default: maxDay = 31; break; } isValid = (day <= maxDay); } if (!isValid) { cout << "Invalid date!"; } else { // Pretty-print the date. string monthName; switch (month) { case 1: monthName = "January"; break; case 2: monthName = "February"; break; case 3: monthName = "March"; break; case 4: monthName = "April"; break; case 5: monthName = "May"; break; case 6: monthName = "June"; break; case 7: monthName = "July"; break; case 8: monthName = "August"; break; case 9: monthName = "September"; break; case 10: monthName = "October"; break; case 11: monthName = "November"; break; case 12: monthName = "December"; break; default: monthName = "Not Known"; break; // Should never happen } string suffix; switch (day) { case 1: case 21: case 31: suffix = "st"; break; case 2: case 22: suffix = "nd"; break; case 3: case 23: suffix = "rd"; break; default: suffix = "th"; break; } stringstream formattedDate; formattedDate << day << suffix << " " << monthName << ", " << year << endl; // Append formatted date to a string that contains all valid dates. allValidDates += formattedDate.str(); cout << allValidDates << endl; } // Ask the user whether he/she wants to enter another date. cout << endl << "Enter another date? " << endl; cin >> response; } while (response == "yes"); // Output all the valid dates entered by the user. cout << "Here are all the valid dates you entered:" << endl; cout << allValidDates << endl; } void main() { // doExercise1Solution(); // doExercise2Solution(); // doExercise3Solution(); doExercise4Solution(); }