i am pretty new to programming and i can't figure out what i'm doing wrong so if someone could please take a look at my code and give me some hints on what i'm doing wrong, that would be awesome!
assignment:
Design a class called Date. The class should store a date in three integers: month, day and year. There should be member functions to print the date in the following forms:
12/25/2012
December 25, 2012
25 December 2012
Demonstrate the class by writing a complete program implementing it. Do not accept values for the day greater than 31 or less than 1. Do not accept values for the month greater than 12 or less than 1.
Modify the Date class you wrote. The class should implement the following exception classes:
InvalidDay-Throw when an invalid day (<1 or >31) is passed to the class.
InvalidMonth-Throw when an invalid month (<1 or >12) is passed to the class.
my code:
Date.h file:
Code:
#pragma once
#include <string>
#include <iostream>
#ifndef DATE_H
#define DATE_H
using namespace std;
class Date
{
private:
int month;
int day;
int year;
public:
Date() {month = 0; day = 0; year = 0;}
Date(int m, int d, int y) {month = m; day = d; year = y;}
int getMonth() {return month;}
void setMonth(int m) {month = m;}
void InvalidMonth(int m)
{if (month >= 1 && month <= 12)
month = m;
else
{ cout << "You have entered an invalid number. Please enter ";
cout << "a number between 1 and 12: " << endl;
cin >> m; }}
int getMonth1() {return month;}
void setMonth1(int m)
{if (m == 1)
{cout << "January";}
else if (m == 2)
{cout << "February";}
else if (m == 3)
{cout << "March";}
else if (m == 4)
{cout << "April";}
else if (m == 5)
{cout << "May";}
else if (m == 6)
{cout << "June";}
else if (m == 7)
{cout << "July";}
else if (m == 8)
{cout << "August";}
else if (m == 9)
{cout << "September";}
else if (m == 10)
{cout << "October";}
else if (m == 11)
{cout << "November";}
else
{cout << "December"; }}
int getDay() const {return day;}
void setDay(int d) {day = d;}
void InvalidDay(int d)
{if (day >= 1 && day <= 31)
day = d;
else
{ cout << "You have entered an invalid number. Please enter ";
cout << "a number between 1 and 31: " << endl;
cin >> d; }}
int getYear() const {return year;}
void setYear(int y)
{if (year <= 1899 || year >= 2014)
year = y;
else
{ cout << "You have entered an invalid number. Please enter ";
cout << "a number between 1900 and 2014: " << endl;
cin >> y; }}
};
#endif
Date.cpp file:
#include "Date.h"
#include <iostream>
#include <string>
using namespace std;
void main() {
Date mdy = Date();
int m;
int d;
int y;
cout << "Please enter the month (1-12): " << endl;
cin >> mdy.setMonth();
cout << "Please enter the day (1-31): " << endl;
cin >> mdy.setDay();
cout << "Please enter the year (1900-2014): " << endl;
cin >> mdy.setYear();
cout << mdy.getMonth() << "/" << mdy.getDay() << "/" << &Date::getYear << endl;
cout << mdy.getMonth1() << " " << mdy.getDay() << ", " << &Date::getYear << endl;
cout << mdy.getDay() << " " << mdy.getMonth1() << " " << &Date::getYear << endl;
system("pause");
}
thanks in advance for any and all help!