using classes for calendar helper

Pages: 12
Really need help on this

L48 I am getting this error "'Day' is missing exception specification 'noexcept'"
what does it mean and how can I fix that?

I know this would work for printing out the day as January 1.. how do I correctly implement the day of the week?
If user chooses to display day 1 I want the output to be "Fri Jan 1, 2021".


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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
// error message
template<typename T> // useful for getting user's keyboard input without crash or hang
T kbd_input(const string &prompt="Enter: ", const string &errmsg="  Oops! Try again.\n\n") {
  while (true) {
    cout << prompt; T ans{};
    if (cin>>ans) return ans;
    else {
      cin.clear(); cin.ignore(INT_MAX, '\n'); cout<<errmsg;
    }
  }
}
const int NUM_MONTHS = 13;

// class
class Day {
  private:
    int day_of_year; // To hold a day
    string month; // To hold a month name
    int arrayIdx; // Stores an array index
    
  public:
    // month:
    static const int daysInMonth[12];
    static const int daysInMonth_accum[12];
    static const string monthsOfYear[NUM_MONTHS];
    enum month_t {jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
    const static int daysInYear[NUM_MONTHS];
    // week
    static const string daysOfWeek[7];
    enum weekday_t {sun, mon, tue, wed, thr, fri, sat};
    // day
    static const int BASE_YEAR=2021;
    static const month_t BASE_MONTH=jan;
    static const weekday_t BASE_WEEKDAY=fri; // January 1, 2021 was a Friday
    static const int BASE_DAY=1;
    explicit Day( int ndays = 1 ) noexcept ; // constructor
    // implement 3 constructors, ++day, day++, --day, day--; "show", "print", "display", << as needed
    Day operator++(int);
    Day operator--(int);
    Day operator++();
    Day operator--();
    // Members can be here (inline) or after.
    
    Day(int d)
    {
        day_of_year = d;
    }

    void print();
};

// initialized monthsOfYear, daysOfWeek, daysInMonth, daysInMonth_accum
const string Day::monthsOfYear[] = {"january", "february",
    "march", "april", "may", "june", "july", "august", "september", "october", "november",
    "december"};
const string Day::daysOfWeek[7] = {"sunday", "monday", "tuesday", "wednesday", "thursday", "friday", "saturday"};
const int Day::daysInMonth[12] = {31, 28, 31, 30,  31,  30,  31,  31,  30,  31,  30,  31};
const int Day::daysInMonth_accum[12] = { 0, 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334};
// limits day to positive numbers after 1 / no error message
Day::Day( int ndays ) noexcept:day_of_year( std::min( 1, ndays ) ) {}


void Day::print()
{
    bool isFound = false;
    
    for (int count = 0; count < NUM_MONTHS && isFound != true; count++)
    {
        if (day_of_year <= daysInYear[count])
        {
            cout << monthsOfYear[count] << " " << (day_of_year %= daysInYear[count-1]);
            isFound = true;
        }
    }
}


int main() {
    int number = 0;
    cout <<"Welcome to ????'s calendar helper!\n";
    Day day; // this is updated in the set day options, and is the 1st day displayed in the Display options.
    for (bool running=true; running; ) {
    cout<<"\nOptions: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: ";
    string option; char option_letter; cin>>option; option_letter=tolower(option[0]);
    switch (option_letter) {
      case 's': // set day, test constructors: Day(), Day(int) constructor
        {
          char option = tolower(kbd_input<char>("  set day d)efault m)month,day n)umber v)ary: "));
          if (option=='q') break;
          
          switch(option) {
            case 'd': {
                cout << "enter a day number (day#=[1]): ";
          //      getline(cin, monthName);
              //  day 1 = Jan 1 2021
              //  day 366 = jan 1, 2022
              break;
            }
            
            case 'm': {
                cout << "enter a month (3-letter-month, like: jan): ";

              break;
            }

            case 'n': {
                cout <<"enter a day of the year (1-365): ";
                // can only change number of day
                // this only changes the day within the year
                // cannot go into next year
              break;
            }
            case 'v': {
                /*
                cout <<"the current day is : "<< 500;
                cout<< "\n+/- number to vary current day: ";
                cin<< // if they enter -100 remove 100 days from current day.
                cout <<"\ncurrent day is now: ";
                cout <<"\nday#=["<<865<<"]"<< mon may 15, 2023;
                // subtracts day from current day. day can be over 365
                //cannot subtract 366 from 365 */
              break;
            }

            default:
              cout<<"Invalid option, try again. q to quit."; break;// continue;
          }
          // cout<<"  day#=["<<day<<"] "<<day.to_string()<<endl; // generic output. Could be helpful.
          break;
        } // case 's' for set day
            
      case 'd': {
          cout<<"You chose to display the current day...\n";

          cout << "Please enter a day (1 through 365): ";
          cin >> number;

          while (number <= 0 || number > 365)
          {
              cout << "Please enter a day (1 through 365): ";
              cin >> number;
          }

          // Create a DayOfYear class object
          Day day_of_year(number);
          day_of_year();

          break;
        }
        
      case 'm': {
          cout<<"You chose to display the current month...\n";
          
          break;
        }
        
      case 'w': {
          cout<<"You chose to display the current week...\n";
          break;
        }
        
      case 't': {
          cout<<"t option - test increment, decrement operators, TO BE IMPLMENTED\n";
          break;
        }
            
      case 'q':
        running=false;
        break;
        
      default:
        cout<<"  Invalid option, try again. q to quit.\n";
        break;
    }
  }
  cout<<"\nThanks for using ??????'s calendar helper!\nGood-bye!\n";
}


should I be working on case s and everything inside it first?
Case s allows the user to set any day in 2021,2022,and 2023.
Options: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: s
set day d)efault m)month,day n)umber v)ary: n
number day of year (1-365...): 500
day#=[500] sun may 15, 2022

The rest just print the day, week, and month based on what the user set the "current day" as
I should be using my base days in this..

Last edited on
Line 48 and line 68 must match. L64 has noexcept, line 48 does not.
See next item also.

Line 40: You're declaring your desstructor constructor. Line 48: You're trying to implement your destructor constructor inside your class. You can do one or the other. You can not do both. If you declare only a signature (L40), your implementation must be outside the class.

Line 149: You declare an object named day_of_year and pass number to it's constructor. That's fine. Line 150: You're try to call the object you just created. What are you trying to do here? Remove this line.

Edit: Corrected typo. "destructor" should have been "constructor".



Last edited on
I've tried to make my code less confusing

I have Set Day done..( case d inside case s)

can someone show me how to print out the week like this
Mon Jun 14, 2021; Tue Jun 15, 2021; Wed Jun 16, 2021; Thr Jun 17, 2021; Fri Jun 18, 2021; Sat Jun 19, 2021; Sun Jun 20, 2021



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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
#include <string>
#include <iostream>
using std::string;
using namespace std;

template<typename T> // useful for getting user's keyboard input without crash or hang
T kbd_input(const string &prompt="Enter: ", const string &errmsg="  Oops! Try again.\n\n") {
  while (true) {
    cout << prompt; T ans{};
    if (cin>>ans) return ans;
    else {
      cin.clear(); cin.ignore(INT_MAX, '\n'); cout<<errmsg;
    }
  }
}


class DayOfYear
{
private:
    const static string monthOfYear[12];
    const static int daysInYear[12];
    const static int daysInMonth[12];
        
    string month;            // To hold a month name
    int day;                    // To hold a day
    int arrayIdx;            // Stores an array index
    int year;

    public:
        DayOfYear()
        { }

        DayOfYear(int d);
        DayOfYear(string, int);
        
        bool isMonthName(string);
        void setMonth(int);
        void helpIncrement();
        void helpDecrement();
        void print();

        string getMonth() const
        { return month; }

        int getDay() const
        { return day; }
    
    int getYear() const
    { return year; }

        DayOfYear operator++();
        DayOfYear operator++(int);
        DayOfYear operator--();
        DayOfYear operator--(int);
};


// Definition of const static array member holding month names
const string DayOfYear::monthOfYear[12] = { "January", "February", "March", "April", "May", "June", "July",
                                                      "August", "September", "October", "November", "December" };

// Definition of const static array member holding a range of days
const int DayOfYear::daysInYear[12] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };
const int DayOfYear::daysInMonth[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };

//
DayOfYear::DayOfYear(int d)
{
    if (d >= 1 && d <= 365)
    {
        setMonth(d);

        if (d > 31)
        {
            d %= daysInYear[arrayIdx-1];
        }
        day = d;
    }
    else
    {
        cout << "\nInput failure: A normal year has at least 1 and no more than 365 days.\n";
    }
}

//
DayOfYear::DayOfYear(string m, int d)
{
    if (isMonthName(m) == true)
    {
        month = m;
    }
    else
    {
        cout << "\nInput failure: " << m << " does not exist\n";
        cout << "Please restart this program and try again!";
        cin.get();
        cin.ignore();
        exit(0);
    }
    
    if (d >= 1 && d <= daysInMonth[arrayIdx])
    {
        day = d;
    }
    else
    {
        cout << "\nInput failure: " << month << " starts with the 1.st, and has "
              << daysInMonth[arrayIdx] << " days.\n";
        cout << "Please restart this program and try again!";
        cin.get();
        cin.ignore();
        exit(0);
    }
}

//
void DayOfYear::setMonth(int d)
{
    bool isFound = false;

    for (int count = 0; count < 12 && isFound != true; count++)
    {
        if (d <= daysInYear[count])
        {
            arrayIdx = count;
            month = monthOfYear[count];
            
            isFound = true;
        }
    }
}

//
bool DayOfYear::isMonthName(string m)
{
    for (int count = 0; count < 12; count++)
    {
        if (m == monthOfYear[count])
        {
            arrayIdx = count;
            return true;
        }
    }
    return false;
}


//
void DayOfYear::print()
{
    cout << getMonth() << " " << getDay() << ", 2021";
}


int main()
{
    int day = 0;
  cout <<"Welcome to ??????s calendar helper!\n";
 // Day day; // this is updated in the set day options, and is the 1st day displayed in the Display options.
  for (bool running=true; running; ) {
    cout<<"\nOptions: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: ";
    string option; char option_letter; cin>>option; option_letter=tolower(option[0]);
    switch (option_letter) {
      case 's': // set day, test constructors: Day(), Day(int) constructor
        {
          char option = tolower(kbd_input<char>("  set day d)efault m)month,day n)umber v)ary: "));
          if (option=='q') break;
          
          switch(option) {
            case 'd': {
              cout<<"    set day d option - TO BE IMPLMENTED\n";
                cout << "Enter a day in the range of 1 through 365:\n";
                cin >> day;
                DayOfYear dayTwo(day);
                dayTwo.print();
              break;
            }
            
            case 'm': {
              cout<<"    set day m option - TO BE IMPLMENTED\n";
              break;
            }

            case 'n': {
              cout<<"    set day n option - TO BE IMPLMENTED\n";
              break;
            }
            
            case 'v': {
              cout<<"    set day v option - TO BE IMPLMENTED\n";
              break;
            }

            default:
              cout<<"    set day Invalid option.\n"; break;// continue;
          }
          // cout<<"  day#=["<<day<<"] "<<day.to_string()<<endl; // generic output. Could be helpful.
          break;
        } // case 's' for set day
        
      case 'd': {

          // Create a second DayOfYear object to demonstrate the overloaded pre- and postfix -- operators
          DayOfYear dayTwo(day);
          dayTwo.print();

          cout << endl;
          break;
        }
        
      case 'm': {
          cout<<"m option - show a month, TO BE IMPLMENTED\n";
          break;
        }
        
      case 'w': {
          cout<<"w option - show a week, TO BE IMPLMENTED\n";
          break;
        }
        
      case 't': {
          cout<<"t option - test increment, decrement operators, TO BE IMPLMENTED\n";
          break;
        }
        
      case 'q':
        running=false;
        break;
        
      default:
        cout<<"  Invalid option, try again. q to quit.\n";
        break;
    }
  }
  cout<<"\nThanks for using ??????s calendar helper! Good-bye!";
}
Last edited on
Topic archived. No new replies allowed.
Pages: 12