using classes for calendar helper

Pages: 12
looking for help on this code I have no idea how to continue from this point
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
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;

template<typename T> // getting user's 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;
    }
  }
}

// expand on this class. This class has one and only one data member. This is an int number
//for day of year. The value 1 represents January 1, 2021. The value 365 represents December
// 31, 2021. The value 366 represents January 1, 2022
class Day {
  private:
    int day_of_year; // limit day_of_year to positive numbers. should not allow day 0, -1, -2, -3
    
  public:
    // month:
    static const int daysInMonth[12];
    static const int daysInMonth_accum[12];
    static const string monthsOfYear[12];  // Initialize to: {"january", "february", "march", ... "december"};
    enum month_t {jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
    
    // week
    static const string daysOfWeek[7];  // Initialized to: {"sunday", "monday", "tuesday", ... "saturday"};
    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;
/*Day of the Year Modification. Provide a 3rd constructor that accepts a string for the month and the day of the month.
Altogether, there are 3 constructors needed for class Day:
1) default constructor – day is Fri Jan 1, 2021
2) constructor provided one int – day is set to that number, but must be
//1 or more.
3) constructor provided month name and month number – day is set to a single int number 1..365
//(assumes year is 2021) by converting the month name and number into a number from 1 to 365.*/
    
  // Much to do: implement 3 constructors, ++day, day++, --day, day--; "show", "print", "display", << as needed
  // Members can be here (inline) or after.
//four overloaded operators
};



class DayOfYear
{
private:
int year;
int day;
string month;
int count=0;
public:
DayOfYear& operator ++();
DayOfYear& operator --();
public:
DayOfYear(char month[], int day)
   {
       for(int i=0;i<12;i++)
       {
           if(month_names(i) == month)
           {
               if( day > 0 && day <= month_days(i))
               {
                   this->month = month;
                   this->day = day;
                   year =0;
                   for(int j=0;j<count;j++)
                   {
                       year=year+ month_days(j);
                   }
                   year=year+day;
                   cout<<"The Day Of The Year is:"<<year<<endl;
               }
               else
               {
                   cout<<"Incorrect date or Day"<<endl;
                   exit(0);
               }
              
           }
           count++;
       }
   }
public:
 string month_names(int month_number)
 {
     string month_names[] ={"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
     return month_names[month_number];
 }
public:
 int month_days(int month_number)
 {
     int month_days[]={31,28,31,30,31,30,31,31,30,31,30,31};
     return month_days[month_number];
 }
};


// Use these as needed:
// 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};
// const string Day::monthsOfYear[12] = {"jan", "feb", "mar", "apr", "may", "jun", "jul", "aug", "sep", "oct", "nov", "dec"};
// const string Day::daysOfWeek[7] = {"sun", "mon", "tue", "wed", "thr", "fri", "sat"};


int main() {
  cout <<"Welcome to ??????s calendar helper!\n";
  Day day;
  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";
              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 'd': {
          cout<<"d option - show a day, TO BE IMPLMENTED\n";
          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!";
}


output:
Options: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: d
Fri Jan 1, 2021

Options: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: w
Fri Jan 1, 2021; Sat Jan 2, 2021; Sun Jan 3, 2021; Mon Jan 4, 2021; Tue Jan 5, 2021; Wed Jan 6, 2021; Thr Jan 7, 2021


Options: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: m
Fri Jan 1, 2021; Sat Jan 2, 2021; Sun Jan 3, 2021; Mon Jan 4, 2021; Tue Jan 5, 2021; Wed Jan 6, 2021; Thr Jan 7, 2021
Fri Jan 8, 2021; Sat Jan 9, 2021; Sun Jan 10, 2021; Mon Jan 11, 2021; Tue Jan 12, 2021; Wed Jan 13, 2021; Thr Jan 14, 2021
Fri Jan 15, 2021; Sat Jan 16, 2021; Sun Jan 17, 2021; Mon Jan 18, 2021; Tue Jan 19, 2021; Wed Jan 20, 2021; Thr Jan 21, 2021
Fri Jan 22, 2021; Sat Jan 23, 2021; Sun Jan 24, 2021; Mon Jan 25, 2021; Tue Jan 26, 2021; Wed Jan 27, 2021; Thr Jan 28, 2021
Fri Jan 29, 2021; Sat Jan 30, 2021; Sun Jan 31, 2021


Options: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: x
Invalid option, try again. q to quit.

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: m
mon day (3-letter-month number, like: jan 1): nov 22
day#=[326] mon nov 22, 2021

Options: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: d
Mon Nov 22, 2021


Options: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: w
Mon Nov 22, 2021; Tue Nov 23, 2021; Wed Nov 24, 2021; Thr Nov 25, 2021; Fri Nov 26, 2021; Sat Nov 27, 2021; Sun Nov 28, 2021
Last edited on
looking for help on this code I have no idea how to continue from this point


Continue to do what? What are trying to do that you don't understand? What's the C++ question?
I mostly just need help on setting up the cout in each case to be able to print what the user wants. I think I set up my classes correctly but im not sure
Last edited on
can anyone help me with how I would initialize this


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
class Day {
  private:
    int day_of_year; /* limit day_of_year to positive numbers.
 Day(){day_of_year =>0;};?          */
    
  public:
    // month:
    static const int daysInMonth[12];
    static const int daysInMonth_accum[12];
    static const string monthsOfYear[12];  /* How to initialize to: {"january", "february", 
"march", "april", "may", "june", "july", "august", "september", "october", "november", 
"december"};*/
    enum month_t {jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};
    

    // week
    static const string daysOfWeek[7];  /* Initialized to: {"sunday", "monday", "tuesday", 
"wednesday", "thursday", "friday", "saturday"};*/
    enum weekday_t {sun, mon, tue, wed, thr, fri, sat};
    
Last edited on
You have to initialize the static data outside of the class definition itself. In C++17 and beyond, I think you can also use the inline keyword.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Example program
#include <iostream>
#include <string>


class Day {
  // ..
  public:

    static const std::string monthsOfYear[12];
};

const std::string Day::monthsOfYear[12] = {"january", "february", 
    "march", "april", "may", "june", "july", "august", "september", "october", "november", 
    "december"};

int main()
{

}

Thanks I have revised the code now...

how can I set a limit to int day_of_year? does this need to be outside the class and main as well?

Day(){day_of_year =>1;};


should not allow day 0, -1, -2, -3, etc..

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
#include <iostream>
#include <string>

class Day {
  private:
    int day_of_year; // limit day_of_year to positive numbers. should not allow day 0, -1, -2, -3
    
  public:
    // month:
    static const int daysInMonth[12];
    static const int daysInMonth_accum[12];
    static const string monthsOfYear[12];  
    enum month_t {jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};

    // 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;
    
  // Much to do: implement 3 constructors, ++day, day++, --day, day--; "show", "print", "display", << as needed
  // Members can be here (inline) or after.
};

// initialized monthsOfYear, daysOfWeek, daysInMonth, daysInMonth_accum
const string Day::monthsOfYear[12] = {"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};



int main() {
}
Last edited on
> how can I set a limit to int day_of_year?

Either correct it silently:
1
2
3
4
5
6
7
8
9
10
11
class Day {

  private:
    int day_of_year; // limit day_of_year to positive numbers. should not allow day 0, -1, -2, -3

  public:
    explicit Day( int ndays = 1 ) noexcept ; // constructor
    // ...
};

Day::Day( int ndays ) noexcept : day_of_year( std::min( 1, ndays ) ) {}


or report an error by throwing an exception:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class Day {

  private:
    int day_of_year; // limit day_of_year to positive numbers. should not allow day 0, -1, -2, -3

  public:
    explicit Day( int ndays = 1 ) ; // constructor
    // ...
};

Day::Day( int ndays ) : day_of_year(ndays) {

    if( day_of_year < 1 ) throw std::invalid_argument( "bad (non-positive) constructor argument" ) ;
}



> does this need to be outside the class and main as well?

It should be done by the class implementation, not outside: the class is responsible for maintaining its invariants.
thank you so much for that explanation that helps a lot
Last edited on
am I doing this part correctly?
Implement 3 constructors, ++day, day++, --day, day--; "show", "print", "display", << as needed

Members can be here (inline) or after.


1
2
3
4
5
6
7
8
class Day{
Public:
Day& operator ++();
Day& operator --();

Private:....

};
For day++ (day--) (post inc/dec) you use:

1
2
Day operator++(int);
Day operator--(int);

could anyone help me with the set day/day modification portion?:

1
2
3
4
5
6
7
8
9
10
11
void DayOfYear::print()
{
    if (day >0)
    {
        if (day <=31)
        {
            cout << January<<" "<< day;
        }
    }
}
??


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
#include <iostream>
#include <iomanip>
#include <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 Day {
  private:
    int day_of_year;
    
  public:
    // month:
    static const int daysInMonth[12];
    static const int daysInMonth_accum[12];
    static const string monthsOfYear[12];
    enum month_t {jan, feb, mar, apr, may, jun, jul, aug, sep, oct, nov, dec};

    // 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);
  // Members can be here (inline) or after.
};

// initialized monthsOfYear, daysOfWeek, daysInMonth, daysInMonth_accum
const string Day::monthsOfYear[12] = {"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 ) ) {}


int main() {
  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";
              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<<"    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<<"d option - show a day, TO BE IMPLMENTED\n";
         
          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!";
}


Example output:


Options: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: d
Fri Jan 1, 2021


Options: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: w
Fri Jan 1, 2021; Sat Jan 2, 2021; Sun Jan 3, 2021; Mon Jan 4, 2021; Tue Jan 5, 2021; Wed Jan 6, 2021; Thr Jan 7, 2021

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: m
mon day (3-letter-month number, like: jan 1): nov 22
day#=[326] mon nov 22, 2021

Options: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: d
Mon Nov 22, 2021


Options: Display d)ay w)eek m)onth; s)et-day t)esting q)uit: w
Mon Nov 22, 2021; Tue Nov 23, 2021; Wed Nov 24, 2021; Thr Nov 25, 2021; Fri Nov 26, 2021; Sat Nov 27, 2021; Sun Nov 28, 2021
Last edited on
L50,51. Where do you take into account leap years?

void DayOfYear::print()

You have the day of the year, you have the accumulated days in month. The month is obtained from iterating this array and comparing the values to the required day.
not taking into account leap years..my assignment says "Leap years can be ignored. The calendar will be good for non-leap years 2021, 2022, 2023"

is there anything else I should add outside my main?

I'm having a hard time trying to come up with my cout lines for each case
Last edited on
Still haven't figure out how to set up the inside of each case but is this all I need on this portion?

1
2
3
4
5
6
7
8
9
10
11
12
class Day {
  private:
    int day_of_year;
    
  public:
    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);
  // Members can be here (inline) or after.
};
I was told to do function over loading in the set date option but couldn't get a clear answer on what would go inside the parenthesis (this example has a 5) if the user is setting the data? wouldn't it be better to use cin lines?

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
#include <iostream>
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 Day {
  private:
    int day_of_year;
    
  public:
};


class printData {
   public:
      void print(int i) {
        cout << "Printing int: " << i << endl;
      }
};

int main(void) {
    printData pd;
    
  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': {
                pd.print(5);
              //  cout << Day::daysOfWeek;
              break;
            }
            
            case 'm': {
                
              break;
            }


   return 0;
          }}}}}


I would appreciate any help as this is due in 3 days
What is option 'd' supposed to do? What is 'm' option supposed to do? You haven't said. All you're asking is what code do you need without saying what that code is supposed to do.

In the Day class you have day_of_year. In words what is supposed to happen to day_of_year for the options. If you can't elaborate on this, you can't code it.

Explain it in sufficient detail that someone who knows nothing about coding can take your instructions and perform the required actions. This is an algorithm.

This whole process is program design. Programs should be first designed before they are coded. Design first then code. Code in small sections and compile and test frequently. Don't progress until you have what you're already coded compiling OK and tested.
For pre/post increment, possibly consider:

1
2
3
4
5
6
7
8
9
10
11
Day& operator++() {
		++day_of_year;
		return *this;
	}

	Day operator++(int) {
		const auto cur {*this};

		++day_of_year;
		return cur;
	}


and similar for pre/post decrement.

The extra copy involved with post inc/dec is why when possible pre-inc/dec should be used to avoid this copy overhead. Obviously for just an int this overhead is minimal, but for other classes it could be significant.
Last edited on
case d should print a day like this :
Fri Jan 1, 2021


case w should print the week like this:
Fri Jan 1, 2021; Sat Jan 2, 2021; Sun Jan 3, 2021; Mon Jan 4, 2021; Tue Jan 5, 2021; Wed Jan 6, 2021; Thr Jan 7, 2021


case m should print out the month like this:
Fri Jan 1, 2021; Sat Jan 2, 2021; Sun Jan 3, 2021; Mon Jan 4, 2021; Tue Jan 5, 2021; Wed Jan 6, 2021; Thr Jan 7, 2021
Fri Jan 8, 2021; Sat Jan 9, 2021; Sun Jan 10, 2021; Mon Jan 11, 2021; Tue Jan 12, 2021; Wed Jan 13, 2021; Thr Jan 14, 2021
Fri Jan 15, 2021; Sat Jan 16, 2021; Sun Jan 17, 2021; Mon Jan 18, 2021; Tue Jan 19, 2021; Wed Jan 20, 2021; Thr Jan 21, 2021
Fri Jan 22, 2021; Sat Jan 23, 2021; Sun Jan 24, 2021; Mon Jan 25, 2021; Tue Jan 26, 2021; Wed Jan 27, 2021; Thr Jan 28, 2021
Fri Jan 29, 2021; Sat Jan 30, 2021; Sun Jan 31, 2021


case s gives the user an opportunity to set a day like this:
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
      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]): ";
             //   day 1 = Jan 1 2021
             //   day 366 = jan 1, 2022
              break;
            }
            
            case 'm': {
                cout << "enter a month (3-letter-month, like: jan): ";
                // I would need to set error message so only 3 letter months are entered 
              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 or days days from current day. 
                //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
/*after the day is set and the user selects to print out the day it should
start from where the date was set not form the base day*/
        



I don't have to take leap years into consideration and this should we good for years 2021,2022, and 2023
Last edited on
I am going to try to understand this part of the code before I move to modifying the date..

could someone help me out and tell me where is my error on L46?
It prints every month with the date after user input except for January

I would also need to add the year to the output. how does that affect what goes into L46?

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
#include <string>
#include <iostream>
using namespace std;

const int NUM_MONTHS = 12;

class DayOfYear
{
    private:
        const static string monthOfYear[NUM_MONTHS];
        const static int daysInYear[NUM_MONTHS];
        
        int day;                // To hold a day

    public:
        DayOfYear(int d)
        {
            day = d;
        }

        void print();
};


// Definition of const static array member holding month names
const string DayOfYear::monthOfYear[] = { "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[] = { 31, 59, 90, 120, 151, 181, 212, 243, 273, 304, 334, 365 };

/* **********************************************************
            DayOfYear::print()
    This function translates a day to month day format and
    outputs it to screen.
   ********************************************************** */

void DayOfYear::print()
{
    bool isFound = false;
    
    for (int count = 0; count < NUM_MONTHS && isFound != true; count++)
    {
        if (day <= daysInYear[count])
        {
            cout << monthOfYear[count] << " " << (day %= daysInYear[count-1]) << ".\n";
            isFound = true;
        }
    }
}

int main()
{
    int number = 0;
    char again = ' ';

    cout << "\t\tDAY OF THE YEAR TO MONTH AND DAY TRANSLATOR\n\n";
    cout << "Please enter a day in the range of 1 through 365, and I will\n"
          << "translate it for you into a month and day format. For instance,\n"
          << "32 becomes February 1., and 230 translates to August 18.\n\n";

    do
    {
        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
        DayOfYear day(number);
        day.print();

        cout << "\nWould you like me to translate another day (y/N)? ";
        cin >> again;

        while (toupper(again) != 'Y' && toupper(again) != 'N')
        {
            cout << "\nWould you like me to translate another day (y/N)? ";
            cin >> again;
        }
        cout << "\n";

        if (toupper(again) == 'N')
        {
            cout << "\nThank you for trying this demo program, have a nice day!";
        }
    } while (toupper(again) != 'N');

    cin.get();
    cin.ignore();
    return 0;
}


output:
DAY OF THE YEAR TO MONTH AND DAY TRANSLATOR

Please enter a day in the range of 1 through 365, and I will
translate it for you into a month and day format. For instance,
32 becomes February 1., and 230 translates to August 18.

Please enter a day (1 through 365): 1
January (lldb)
Last edited on
On line 46, If count is zero the program attempts to access memory before the beginning of the array, particularly it accesses daysInYear[-1].

People conventionally start counting from one, but computers start counting from zero. There are good reasons for this behavior.
https://www.cs.utexas.edu/users/EWD/transcriptions/EWD08xx/EWD831.html

You can avoid these mistakes in the future by always double checking code where you do any arithmetic on array indices. Basically you should have double checked what you were doing as soon as you wrote count - 1.

One solution is to add a zeroth month along with a corresponding entry in daysofyear

1
2
3
4
const string DayOfYear::monthOfYear[] = { "", "January", "February", "March", "April", "May", "June", "July",
                                                      "August", "September", "October", "November", "December" };

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


And then to use subtraction instead of the modulo operator on line 46 to remove the singularity at zero:
cout << monthOfYear[count] << " " << (day - daysInYear[count-1]) << ".\n";
Last edited on
Pages: 12