I feel like an idiot asking for help every step of the way. My program is almost complete. I just can't seem to wrap my head around the constructor issue.
Here is the check list of what I have left:
Appropriately use the Dates(int,int,int) & the Dates(string,int,int) constructors;
Appropriately use the displayDates function;
Appropriately use the compareDates function (which I'm sure I can do once I get the other stuff done)
If I need to change the data type of my month data member please let me know...
My main concern with this is getting it to read the string months and display the date. It will read the number values of the months and convert it to the string value. However, I also need it to read in the string value of the month and display the string value of the month. The program will read until it reaches a string value and then it stops without reading the rest of the file. Any suggestions??
Here is the class set up:
#ifndef DATE_H
#define DATE_H
#include <string>
using namespace std;
class Date
{
public:
//Member Functions
//Constructors
Date();
Date(int,int,int);
Date(string,int,int);
//is the date valid
bool isValidDate(int,int,int);
//convert int to string
string convertMonthNumberToString();
//dsplays entire date using convertMonthNum... if necessary
void displayDate();
//compare the dates
int compareDates(int,int,int);
//display day number
int dayNumber();
//increment
void increment();
//is it a leap year
bool isLeapYear();
//setter
void setDate(int,int,int);
//getter
int getMonth() const;
int getDay() const;
int getYear() const;
private:
//Data members
int month;
int day;
int year;
};
#endif //DATE_H
Here is the class implementation:
#include "testerResult.h"
#include <iostream>
#include <string>
using namespace std;
Date::Date()
{
month = 0;
day = 0;
year = 0;
}
//Not sure if this is correct...
Date::Date(int m, int d, int y)
{
m = month;
d = day;
y = year;
}
//was previously informed that this is incorrect
Date::Date(string m, int d, int y)
{
m = convertMonthNumberToString();
day = d;
year = y;
}
//I think i need an if statement in here
void Date::displayDate()
{
cout << convertMonthNumberToString() << " " << day << ", " << year;
}
//can probably figure this out after I get the other things done
int Date::compareDates(int m, int d, int y)
{
return 0;
}
string Date::convertMonthNumberToString()
{
switch (month)
{
case 1: return "January";
case 2: return "February";
case 3: return "March";
case 4: return "April";
case 5: return "May";
case 6: return "June";
case 7: return "July";
case 8: return "August";
case 9: return "September";
case 10: return "October";
case 11: return "November";
case 12: return "December";
}
}
int Date::dayNumber()
{
int numOfDaysInMonth[12]={31,28,31,30,31,30,31,31,30,31,30,31};
int dayNum = 0;
for(int i = 1; i < month; i++)
{
dayNum += numOfDaysInMonth[i - 1];
}
return dayNum += day;
}
void Date::increment()
{
if(day < 31)
{
day++;
}
else if(month < 12)
{
month++;
day = 1;
}
else
{
year++;
month = 1;
day = 1;
}
if((isLeapYear() == true) && (month == 2 && day == 30))
{
month++;
day = 1;
}
else if((isLeapYear() == false) && (month == 2 && day == 29))
{
month++;
day = 1;
}
}
bool Date::isLeapYear()
{
bool result;
if((year % 4 == 0) && (year % 100 != 0) || (year % 400 == 0))
{
result = true;
}
else
{
result = false;
}
return result;
}
bool Date::isValidDate(int m, int d, int y)
{
if(m < 1 || m > 12)
{
return false;
}
else if(d < 1 || d > 31)
{
return false;
}
else if(y < 1800 || y > 2100)
{
return false;
}
else
{
return true;
}
}
void Date::setDate(int m, int d, int y)
{
if(m < 1 || m >12) return;
month = m;
if(d < 1 || d > 31) return;
day = d;
if(y < 1800 || y > 2100) return;
year = y;
}
int Date::getMonth() const
{
return month;
}
int Date::getDay() const
{
return day;
}
int Date::getYear() const
{
return year;
}