1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
|
#include <iostream>
#include <cstdlib>
using namespace std;
class DayOfYear
{
private:
int julianDay;
// UPDATE: previously "string month[12]".
static const string month[12];
public:
DayOfYear( int );
void setJulianDay( int );
void print();
};
// UPDATE: month is a static const and properly initialized per ne555 posts.
const string DayOfYear::month[12] = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };
DayOfYear::DayOfYear( int day = 1 )
{
julianDay = day;
/* WRONG
month[12] = { "January", "February", "March",
"April", "May", "June", "July",
"August", "September", "October",
"November", "December" };
*/
}
void DayOfYear::setJulianDay( int jd )
{
if ( jd > 0 && jd < 365 )
julianDay = jd;
else
{
cout << "Invalid Julian Day. Terminating program...\n";
exit(EXIT_FAILURE);
}
}
void DayOfYear::print()
{
if ( julianDay <= 31 ) // UPDATE: Fixed out of bounds subscripts as ne555 pointed out.
cout << month[0] << " " << julianDay;
else if ( julianDay <= 59 )
cout << month[1] << " " << julianDay - 31;
else if ( julianDay <= 90 )
cout << month[2] << " " << julianDay - 59;
else if ( julianDay <= 120 )
cout << month[3] << " " << julianDay - 90;
else if ( julianDay <= 151 )
cout << month[4] << " " << julianDay - 120;
else if ( julianDay <= 181 )
cout << month[5] << " " << julianDay - 151;
else if ( julianDay <= 212 )
cout << month[6] << " " << julianDay - 181;
else if ( julianDay <= 243 )
cout << month[7] << " " << julianDay - 212;
else if ( julianDay <= 273 )
cout << month[8] << " " << julianDay - 243;
else if ( julianDay <= 304 )
cout << month[9] << " " << julianDay - 273;
else if ( julianDay <= 334 )
cout << month[10] << " " << julianDay - 304;
else
cout << month[11] << " " << julianDay - 334;
}
int main()
{
int day;
DayOfYear date;
cout << "\nPlease enter a Julian Day: ";
cin >> day;
date.setJulianDay( day );
date.print();
cout << endl << endl;
return 0;
}
|