Hey I could really use somebody's help in explaining what I did wrong to get compiler errors with a "month" class I wrote for school. I am new to C++ so it's probably just some silly error or I am trying to do something completely wrong. I am just trying to get this thing to compile so you needn't debug it. Anyway all help will be greatly appreciated. EDIT: Many of my errors concern the declaration of monthAbbrev, does anyone see what I am doing wrong with it?
And the implementationCode:#ifndef MONTH_H #define MONTH_H const short MAX_M = 12; const short MAX_N = 4; class Month { short monthNum; const char monthAbbrev[MAX_M][MAX_N] = { "jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec" }; void translate(const char[]); public: Month(); Month(const char name[]); Month(const short number); void input(void); void outputNumber(void) const; void outputName(void) const; Month nextMonth(void) const; bool compare(const char str1[], const char str2[]) const; short get_monthNum(void) const { return monthNum; } void set_monthNum(const short num) { monthNum = num; } }; #endif
I'm new to this forum also, so I hope I posted that code correctly and didn't break any of the forum's rules. BTW I posted all the code so you could maybe compile it and tell me what errors you get.Code:#include <iostream> #include <cstring> #include <cctype> using namespace std; #include "month.h" Month::Month() { monthNum = 1; } Month::Month(const char name[]) { translate(name); } Month::Month(const short number) { monthNum = number; } void Month::input(void) { char name[MAX_N]; cin >> monthNum; if(cin.fail()) //if fail must be a char { cin.clear(); cin.getline(name, MAX_N); //get string name[MAX_N - 1] = '\0'; translate(name); //translate name to set monthNum } return; } void Month::outputNumber(void) const { cout << monthNum; return; } void Month::outputName(void) const { cout << monthAbbrev[monthNum - 1]; return; } Month Month::nextMonth(void) const { Month temp(monthNum + 1); return temp; } bool Month::compare(const char str1[], const char str2[]) const { bool same = false; short index = 0; while(index != (MAX_N) && !same) { if(tolower(str1[index]) == tolower(str2[index])) { same = true; while(index != (MAX_N) && same) { if(tolower(str1[index]) != tolower(str2[index])) { same = false; } index++; } } } return same; } void Month::translate(const char[] name) { for(short i = 0; i != MAX_M; i++) { if(compare(monthAbbrev[i], name)) { monthNum = i; } } return; }



LinkBack URL
About LinkBacks


