0

The goal of this program is to take an integer from 1 - 365 and convert it into a month and day. Ex. 2 = January 2nd, 365 = December 31st. I thought I got everything typed out correctly, I was on the right track but now I have like 16 errors coming from lines 12-14 in my header file, and 1 error coming from my class file.

I'm not sure if it's even truly the header or if I messed up somewhere else.

HEADER FILE

#ifndef DAYOFYEAR_H
#define DAYOFYEAR_H

class DayOfYear
{
private:
    int day;

public:
    DayOfYear();
    static int daysAtEndOfMonth[];
    static string monthName[];
    void print();
    void setDay(int day) { this->day = day; };

};

#endif

CLASS

#include "DayOfYear.h"
#include <iostream>
#include <string>

using namespace std;

DayOfYear::DayOfYear() {

}

int DayOfYear::daysAtEndOfMonth[] = {
31, 59, 90,
120, 151, 181,
212, 243, 273,
304, 334, 365
};

string DayOfYear::monthName[] = { "January", "February",
"March", "April",
"May", "June"
"July", "August",
"September", "October",
"November", "December"
};

void DayOfYear::print() {
    int month = 0;

    while (daysAtEndOfMonth[month] < day)
        month = (month + 1) % 12;


    if (month == 0) {
        cout << "\nJanuary" << day << endl << endl;
    }

    else
    {
        cout << endl << monthName[month] << " " << day - daysAtEndOfMonth[month - 1]
            << "\n\n";
    };
};

MAIN

#include <iostream>
#include <string>
#include "DayOfYear.h"

using namespace std; 

int main() {
    int day; 
    DayOfYear DayOfYearObj;

    cout << "This program will convert an integer between 1 and 365 to a Month/Day format"
        << endl << endl;
    cout << "Please enter an integer from 1 to 365:";
    cin >> day;

    //set day
    DayOfYearObj.setDay(day);

    //display
    DayOfYearObj.print();

    return 0;

}
halfer
  • 19,824
  • 17
  • 99
  • 186
  • 2
    Please include the actual errors that you're getting, but my crystal ball tells me that [this](http://stackoverflow.com/questions/11300652/static-data-member-initialization) is what you need. – SingerOfTheFall Feb 13 '17 at 06:39
  • 2
    Lines 12-14 are not written correctly. (When you say "you have errors" the only answer one can possibly give you is "then you must have done something wrong".) – Mike Nakis Feb 13 '17 at 06:40
  • voted to close for failure to proivide MVCE. – Walter Feb 13 '17 at 08:54
  • Use constexpr for your static arrays, put their values in the header and thank me later. – Guillaume Racicot Feb 13 '17 at 14:41

1 Answers1

2

Your header file needs to #include <string>, and you must refer to std::string in the header file.

(Using using namespace std; is bad enough in a C++ file, it is much, much worse in a header file - it brings the whole of the std namespace into the global namespace in every file that includes your header. Just say no!)

Other comments:

  • daysAtEndOfMonth and monthName should be const.
  • Next time, include the error message (this error was instantly obvious, the next one won't be).
  • Next time, put a comment marking which is line with the error - I'm not going to take the time to count down to line 45.