Alright! Finished code for the most part. Lay it on me.
Main:
Code
#include <iostream>
#include "Date.h"
bool getMonthLength(int month, int year, bool&, bool&, bool&);
bool inputValidation(int minInputRange, int maxInputRange, int& userInput);
bool checkDays(int day, bool isLeapYear, bool shortMonth, bool longMonth);
/***************************************************
Gathers user input and will display the three
dates upon validation of input
****************************************************/
int main()
{
bool isLeapYear,
shortMonth,
longMonth;
int day,
month,
year;
const int MAX_YEAR = 2020,
MIN_YEAR = 1950,
MAX_MONTH = 12,
MIN_MONTH = 1,
MIN_MONTH_LENGTH = 1,
MAX_NON_LEAP_MONTH = 28,
MAX_LEAP_MONTH = 29,
MAX_SHORT_MONTH = 30,
MAX_LONG_MONTH = 31;
std::cout << "Enter the month(Numerical): ";
std::cin >> month;
while(inputValidation(MIN_MONTH, MAX_MONTH, month))
std::cin >> month;
std::cout << "Enter the year: ";
std::cin >> year;
while(inputValidation(MIN_YEAR, MAX_YEAR, year))
std::cin >> year;
//Determines month and leap year
getMonthLength(month, year, isLeapYear, shortMonth, longMonth);
std::cout << "Enter the day: ";
std::cin >> day;
//Determines if day input is valid based on the given range
if(!isLeapYear)
while(inputValidation(MIN_MONTH_LENGTH, MAX_NON_LEAP_MONTH, day))
{
std::cout << "Min/Max values are: " << MIN_MONTH_LENGTH << "/" << MAX_NON_LEAP_MONTH
<< std::endl << "Enter a value: " << std::endl;
std::cin >> day;
}
else if(isLeapYear)
while(inputValidation(MIN_MONTH_LENGTH, MAX_LEAP_MONTH, day))
{
std::cout << "Min/Max values are: " << MIN_MONTH_LENGTH << "/" << MAX_LEAP_MONTH
<< std::endl << "Enter a value: " << std::endl;
std::cin >> day;
}
else if(shortMonth)
while(inputValidation(MIN_MONTH_LENGTH, MAX_SHORT_MONTH, day))
{
std::cout << "Min/Max values are: " << MIN_MONTH_LENGTH << "/" << MAX_SHORT_MONTH
<< std::endl << "Enter a value: " << std::endl;
std::cin >> day;
}
else if(longMonth)
while(inputValidation(MIN_MONTH_LENGTH, MAX_LONG_MONTH, day))
{
std::cout << "Min/Max values are: " << MIN_MONTH_LENGTH << "/" << MAX_LONG_MONTH
<< std::endl << "Enter a value: " << std::endl;
std::cin >> day;
}
Date myDate(day, month, year); //creates myDate object
myDate.showShortDate(); //print short date
myDate.showLongDate(); //print long date
myDate.showEuroDate(); //print euro date
/*
Date noArgDate; //creates noArgDate object
noArgDate.showShortDate(); //print short date
noArgDate.showLongDate(); //print long date
noArgDate.showEuroDate(); //print euro date
*/
return 0;
}
/***************************************************
Sets a bool based on which month it is, and
another bool based on whether a leap year has
occurred
****************************************************/
bool getMonthLength(int month, int year, bool& isLeapYear, bool& shortMonth, bool& longMonth)
{
enum monthNameList { January = 1, February, March, April, May, June, July, August, September, October, November, December };
isLeapYear = shortMonth = longMonth = false;
//Determines leap year
if((year % 4) == 0)
{
if((year % 100) != 0)
return isLeapYear = true;
else if(((year % 100) == 0) && ((year % 400) == 0))
return isLeapYear = true;
else if (((year % 100) == 0) && ((year % 400) != 0))
return isLeapYear;
}
//Determines month
if(month == April || month == June || month == September || month == November)
return shortMonth = true;
else if(month == February)
{
if(isLeapYear)
return isLeapYear = true;
else
return isLeapYear;
}
else if(month == January || month == March || month == May || month == July || month == August || month == September || month == October || month == December)
return longMonth = true;
}
/***************************************************
Validates the user's input based on the min/max
arguments sent by the function call
****************************************************/
bool inputValidation(int minInputRange, int maxInputRange, int& userInput)
{
if(userInput > maxInputRange || userInput < minInputRange)
return true;
else
return false;
}
Date.h:
Code
#ifndef DATE_H_INCLUDED
#define DATE_H_INCLUDED
class Date
{
private:
int m_day,
m_month,
m_shortYear,
m_year;
public:
Date()
{
m_day = 1;
m_month = 1;
m_year = 2001;
}
Date(int day, int month, int year);
const int getDay();
const int getMonth();
const int getYear();
int makeShortYear();
void makeMonthName(int month);
void showShortDate();
void showLongDate();
void showEuroDate();
};
#endif // DATE_H_INCLUDED
Date.cpp:
Code
#include <iostream>
#include "Date.h"
/***************************************************
Constructor initalizes private member variables to
their "twins" inside main which have been validated
****************************************************/
Date::Date(int day, int month, int year)
{
m_day = day;
m_month = month;
m_year = year;
}
/***************************************************
Will return private member variable m_day
****************************************************/
const int Date::getDay()
{
return m_day;
}
/***************************************************
Will return private member variable m_month
****************************************************/
const int Date::getMonth()
{
return m_month;
}
/***************************************************
Will return private member variable m_year
****************************************************/
const int Date::getYear()
{
return m_year;
}
/***************************************************
Prints the short version of a date: 1/1/01
Will work only for dates greater than 1899 and less
than 2100
****************************************************/
void Date::showShortDate()
{
std::cout << getMonth() << "/" << getDay() << "/";
if(makeShortYear() == 0) //in case of year 2000, will add another 0 to display "00"
std::cout << "0" << makeShortYear() << std::endl;
else
std::cout << makeShortYear() << std::endl;
}
/***************************************************
Prints the long version of a date: January 1, 2001
****************************************************/
void Date::showLongDate()
{
makeMonthName(m_month);
std::cout << " " << getDay() << ", " << getYear() << std::endl;
}
/***************************************************
Prints the euro version of a date: 1 January 2001
****************************************************/
void Date::showEuroDate()
{
std::cout << getDay() << " ";
makeMonthName(m_month);
std::cout << " " << getYear() << std::endl;
}
/***************************************************
Prints the name of the month based on the value
entered by the user that was validated in main
****************************************************/
void Date::makeMonthName(int monthValue)
{
switch(monthValue)
{
case 1:
std::cout << "January";
break;
case 2:
std::cout << "February";
break;
case 3:
std::cout << "March";
break;
case 4:
std::cout << "April";
break;
case 5:
std::cout << "May";
break;
case 6:
std::cout << "June";
break;
case 7:
std::cout << "July";
break;
case 8:
std::cout << "August";
break;
case 9:
std::cout << "September";
break;
case 10:
std::cout << "October";
break;
case 11:
std::cout << "November";
break;
case 12:
std::cout << "December";
break;
}
}
/***************************************************
Create the two digit version of the year for
the short version of date. Will only work for values
1900-2099
****************************************************/
int Date::makeShortYear()
{
int shortYearTemp = getYear();
if(shortYearTemp > 1999)
return shortYearTemp - 2000;
else if (shortYearTemp < 2000)
return shortYearTemp - 1900;
}
This post was edited by cssa on Oct 23 2013 04:18am