if statements and calendar

I need help, how do i get my month to output 31 days after february? everything after that just gives me 28 days or 29, if someone could look at my code it would really help me. The If function i am talking about is in the computeDays() function.. if anyone could please run the program and let me know i would really apreciate it. been workig on this for the past 11 hours i kid you not!

// this is what it looks like
// leapyear works but after february it wont output the 30/31 days and yes i have tried adding else before my other if statements.

Enter a month number: 5
Enter year: 2001

May, 6297280
Su Mo Tu We Th Fr Sa
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

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

/***********************************************************************
 * This will prompt user for month number!
 **********************************************************************/
int getMonth()
{
   int month;
   cout << "Enter a month number: ";
   cin >> month;

   while (month < 1 or month > 12)
   {
      cout << "Month must be between 1 and 12." << endl;
      cout << "Enter a month number: ";
      cin >> month;
   }
   return month;
}

/********************************************************************
 * This function will prompt for year!
 **********************************************************************/
int getYear()
{
   int year;
   cout << "Enter year: ";
   cin >> year;

   while (year < 1753)
   {
      cout << "Year must be 1753 or later." << endl;
      cout << "Enter year: ";
      cin >> year;
   }
   cout<< endl;
   return year;
 }

/*****************************************************
 * give month their names!
 ******************************************************/
void monthset(int month, int year)
{
   if (month == 1)
      cout << "January, " << year << endl;
   if (month == 2)
      cout << "February, " << year << endl;
   if (month == 3)
      cout << "March, " << year << endl;
   if (month == 4)
      cout << "April, " << year << endl;
   if (month == 5)
      cout << "May, " << year << endl;
   if (month == 6)
      cout << "June, " << year << endl;
   if (month == 7)
      cout << "July, " << year << endl;
   if (month == 8)
      cout << "August, " << year << endl;
   if (month == 9)
      cout << "September, " << year << endl;
   if (month == 10)
      cout << "October, " << year << endl;
   if (month == 11)
      cout << "November, " << year << endl;
   if (month == 12)
      cout << "December, " << year << endl;
}
/******************************************************
 * compute the days!
 *****************************************************/
int computeDays(int month, int year)
{
   bool isLeapYear =
      (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);

   int days31 = 31;
   int days30 = 30;
   {
      if (month == 1)
         return days31;
      if (month == 2 and isLeapYear)
         return 29;
      else
            return 28;
      if (month = 3)
         return days31;
      if (month == 4)
         return days30;
      if (month == 5)
         return 31
      if (month == 6)
         return 30;
      if (month == 7)
         return 31;
      if (month == 8)
         return 31;
      if (month == 9)
         return 30;
      if (month == 10)
if (month == 11)
         return 30;
      else
      if (month == 12)
      return 31;
   }
}

/**********************************************************
 * lets make it pretty with display!
 *********************************************************/
void display(int month, int days)
{
   cout << "  Su  Mo  Tu  We  Th  Fr  Sa" << endl;

   int pos = 1;

   month = (month +0)%7;

   for (int s = 0; s < month; s++, pos++)
   {
      cout << "    ";
   }

   for (int j = 1; j <= days; j++, pos++)
   {

      if (pos%8==0)
      {
         pos = 1;
         cout << endl;
      }
      cout << setw(4) << j;

   }

   cout << endl;
}

/**********************************************************************
 * Main Function will output everything!
 ***********************************************************************/
int main()
{
   // Get Month
   int month = getMonth();
   // Get Year
   int year = getYear();
   // Get Offset
   monthset(month, year);
   // Get days
   int days = computeDays(month, year);
   // Get display
   display (month, days);

   return 0;
}
Last edited on
closed account (48T7M4Gy)
lines 86 and 88 are the problem.
^ what he said. Easy fix.
closed account (48T7M4Gy)
oops! and lines 85 and 89 are downmarket too
Last edited on
closed account (48T7M4Gy)
if (month == 2)
{
if( isLeapYear )
return 29;
else
return 28;
}
Topic archived. No new replies allowed.