Calendar Program

I'm writing a calendar program that ask for the the number of the month and the year and displays a calendar. My problem is that when it tries to print the calendar but the infinitely loops the numbers, I can't get the offset right, nether can I get the calendar header to display the right month or year.

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

int daysYear(int yearNumber);
int daysMonth(int monthNumber);
int offset(int yearNumber, int monthNumber, int numberofDaysinaMonth, int years);

bool isLeapYear(int yearNumber)
{
  if(yearNumber % 4 == 0 && yearNumber % 100 != 0)
    return true;
  else if(yearNumber % 400 == 0)
        return true;
  else
    return false;
}

int getMonth()
{
  int monthNumber;
  for (int a=0; a<1;)
    {
      cout << "Enter a month number: ";
      cin >> monthNumber;
      if (monthNumber < 0 || monthNumber > 12)
        {
          cout << "Month must be between 1 and 12.\n";
          monthNumber = 0;
          continue;
        }
      a = 1;
    }

  return monthNumber;

}
int getYear()
{
  int yearNumber;
  for(int b=0; b<1;)
    {
      cout << "Enter year: ";
      cin >> yearNumber;
      if(yearNumber < 1753)
        {
          cout << "Year must be 1753 or later.\n";
          yearNumber = 0;
          continue;
        }
      b = 1;
    }
  return yearNumber;
}

int offset(int yearNumber, int monthNumber, int numberofDaysinaMonth,
           int years)
{
  int days = 0;
  int offsetNumber;
  for(int b = 1753; b < yearNumber; b++)
    {
          days = days + years;
    }
  for(int c = 1; c < monthNumber; c++)
    {
      days = days + numberofDaysinaMonth;
    }
      offsetNumber = days % 7;
      return offsetNumber;
}

int daysMonth(int monthNumber, int yearNumber)
{
  int numberofDaysinaMonth;

  if(monthNumber = 1)
    numberofDaysinaMonth = 31;
  else if(monthNumber = 2)
    {
      if(isLeapYear(yearNumber))
        {
           numberofDaysinaMonth = 28;
        }
      else
           numberofDaysinaMonth = 29;
    }
    else if(monthNumber = 3)
        numberofDaysinaMonth = 31;
    else if(monthNumber = 4)
        numberofDaysinaMonth = 30;
    else if(monthNumber = 5)
        numberofDaysinaMonth = 31;
    else if(monthNumber = 6)
        numberofDaysinaMonth = 30;
    else if(monthNumber = 7)
        numberofDaysinaMonth = 31;
    else if(monthNumber = 8)
        numberofDaysinaMonth = 31;
    else if(monthNumber = 9)
        numberofDaysinaMonth = 30;
    else if(monthNumber = 10)
        numberofDaysinaMonth = 31;
    else if(monthNumber = 11)
       numberofDaysinaMonth = 30;
    else if(monthNumber = 12)
       numberofDaysinaMonth = 31;
       return numberofDaysinaMonth;
}
int daysYear(int yearNumber)
{
  int years;
  if(isLeapYear(yearNumber))
    years = 366;
  else
    years = 365;
}

void display(int monthNumber,int yearNumber, int offsetNumber, int numberofDays\
inaMonth)
 {
   if(monthNumber = 1)
      cout << "January, " << yearNumber << endl;
   else if(monthNumber = 2)
      cout << "Febuary, " << yearNumber << endl;
   else if(monthNumber = 3)
      cout << "March, " << yearNumber << endl;
   else if(monthNumber = 4)
      cout << "April, " << yearNumber << endl;
   else if(monthNumber = 5)
      cout << "May, " << yearNumber << endl;
   else if(monthNumber = 6)
      cout << "June, " << yearNumber << endl;
   else if(monthNumber = 7)
      cout << "July, " << yearNumber << endl;
   else if(monthNumber = 8)
      cout << "August, " << yearNumber << endl;
   else if(monthNumber = 9)
      cout << "September, " << yearNumber << endl;
   else if(monthNumber = 10)
      cout << "October, " << yearNumber << endl;
   else if(monthNumber = 11)
      cout << "November, " << yearNumber << endl;
   else if(monthNumber = 12)
      cout << "December, " << yearNumber << endl;
   cout << endl;
  cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";
   if(offsetNumber == 0)
     cout << setw(4) <<" ";
   else if(offsetNumber == 1)
     cout << setw(8) <<" ";
   else if(offsetNumber == 2)
     cout << setw(12) <<" ";
   else if(offsetNumber == 3)
     cout << setw(16) <<" ";
   else if(offsetNumber == 4)
     cout << setw(20) <<" ";
   else if(offsetNumber == 5)
     cout << setw(24) <<" ";
   else if(offsetNumber == 6)
     cout << " ";
   for(int d = 1; d <= numberofDaysinaMonth; d++)
     {
       if((offsetNumber + d) % 7 == 0)
         cout << endl;
       cout << setw(4) << d;
     }


 }

int main(int monthNumber, int yearNumber, int offsetNumber, int numberofDaysina\
Month)
{
  getMonth();
  getYear();
  display(monthNumber, yearNumber, offsetNumber, numberofDaysinaMonth);
   return 0;
}


Last edited on
Put your code in [code] tags and use proper indentation. It's hard to read like this.
I don't know why but I properly indent but when I submit the message it drags everything to the side and what are [code] tags.
There we go I figured it out. Now I could really use some help.
I'll look into it in a moment, but before that,
why does your main take arguments?

* daysYear doesn't return anything.
* daysMonth: declaration different from definition

the main problem: you don't assign valuer returned by your functions to your locals in main. your main should be
1
2
3
4
5
6
7
8
9
10
11
int main()
{
	int monthNumber = getMonth();
	int yearNumber = getYear();
	int numberofDaysinaMonth = daysMonth(monthNumber, yearNumber);
	int offsetNumber = offset(yearNumber, monthNumber, numberofDaysinaMonth, 1);//was the last argument supposed to be daysYear(yearNumber) ?
  
	display(monthNumber, yearNumber, offsetNumber, numberofDaysinaMonth);
	cin.ignore().get();
	return 0;
}
Last edited on
Yea the last arguement was suppose to be daysYear(yearNumber).
I fixed my main but my offset is still off and the header still displays january.
That may be because in daysMonth() and display() in your ifs you use = instead of ==.
By the way, using arrays could save you a lot of code.
Topic archived. No new replies allowed.