I had all of my code in working order when it was all just in one file. Once I tried to separate it into a header and another source file, it does not work anymore. I hope hoping that someone could help me out and steer me in the right direction with this. Thank you.
Here is my Header file:
Code:
#ifndef DATE_H
#define DATE_H
#include <iostream>
#include <iomanip>
#include <string>
using std::cout;
using std::endl;
using std::setfill;
using std::setw;
using std::string;
class Date
{
public:
// This is the default constructor
Date(int mm = 1, int dd = 1, int yy = 2000)
{
setMonth(mm);
setDay(dd);
setYear(yy);
};
// Function that will set the month
void setMonth(int mm);
// Function that will set the day
void setDay(int dd);
// Function that will set the year
void setYear(int yy);
// Function that will get the month
int getMonth();
// Function that will get the day
int getDay();
// Function that will get the year
int getYear();
// Function that will get the name of the month when the right number is selected.
std::string getMonthName();
// Print function that outputs date in numeric format
void print();
//rint function that outputs date in long format.
void printLong();
private:
int month;
int day;
int year;
};
#endif
Here is my main.cpp:
Code:
#include <iostream>
#include <iomanip>
#include <string>
#include "date.h"
using std::cout;
using std::endl;
using std::setfill;
using std::setw;
using std::string;
//#include "Date.h"
// note - you may need to change the definition of the main function to
// be consistent with what your C++ compiler expects.
//int _tmain(int argc, _TCHAR* argv[])
int main()
{
Date d1; // default ctor
Date d2(7, 4, 1976); // July 4'th 1976
Date d3(0, 15, 1880);// Adjusted by ctor to January 15'th 1900
d1.print(); // prints 01/01/2000
d1.printLong(); // prints 1 January 2000
cout << endl;
d2.print(); // prints 07/04/1976
d2.printLong(); // prints 4 July 1976
cout << endl;
d3.print(); // prints 01/15/1900
d3.printLong(); // prints 15 January 1900
cout << endl;
cout << "object d2's day is " << d2.getDay() << endl;
cout << "object d2's month is " << d2.getMonth() << " which is " << d2.getMonthName() << endl;
cout << "object d2's year is " << d2.getYear() << endl;
return 0;
}
and here is my source cpp:
Code:
#include "date.h"
void setMonth(int mm)
{
month = (mm);
}
void setDay(int dd)
{
day = (dd);
}
void setYear(int yy)
{
year = (yy);
}
int getMonth()
{
// If month is under 1 and over 12, then month = 1
if (month < 1 || month > 12)
{
month = 1;
}
return month;
}
int getDay()
{
// If month equals 2 and day is greater than 28, then day = 1.
if (month == 2 && day > 28)
{
day = 1 ;
}
// If the day is less than 1 and greater than 31 and is one of the listed months, then day = 1.
if ((day < 1 || day>31) && (month==1 || month ==3 || month == 5 || month == 7 || month== 8 || month== 10 || month==12))
{
day = 1 ;
}
// If the day is less than 1 and greater than 30 and is one of the listed months, then day = 1.
if ((day < 1 || day>30) && (month==4 || month ==6 || month == 9 || month == 11))
{
day = 1 ;
}
return day;
}
int getYear()
{
if (year < 1900)
year = 1900;
return year;
}
string getMonthName()
{
string MonthName;
if (month == 1)
MonthName = ("January");
if (month == 2)
MonthName = ("February");
if (month == 3)
MonthName = ("March");
if (month == 4)
MonthName = ("April");
if (month == 5)
MonthName = ("May");
if (month == 6)
MonthName = ("June");
if (month == 7)
MonthName = ("July");
if (month == 8 )
MonthName = ("August");
if (month == 9)
MonthName = ("September");
if (month == 10)
MonthName = ("October");
if (month == 11)
MonthName = ("November");
if (month == 12)
MonthName = ("December");
return MonthName;
}
void print() //
{
//Call the functions to output.
cout << "The date is \n" << getMonth() << "/" << getDay() << "/" << getYear()<< endl;
}
// Print function that outputs date in long format.
void printLong()
{
// Calls the functions to output
cout << "The date is \n"<< getDay() << " " << getMonthName() << " " << getYear()<< endl;
}
please! anything would be amazing. THank you