calendar program help

I have a calendar program pretty much all the way built, but for whatever reason the day the first falls on is off on about half of the months.
This is my code so far:
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
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>

using namespace std;

const string MONTHARRAY[12] = {  "            January", "           February", "            March", "            April", "           May", "           June", "           July", "           August", "           September", "           October", "           November", "           December"};
const string DAYARRAY[7] = {"Sun ", " Mon ", " Tue ", " Wed ", " Thu ", " Fri ", " Sat"};
int a,y,day,m,d, x;
int month;

void printMonth(int month, int startDay, int year);
int  getFirst(int month, int day, int year);

int main (void)
{
    cout<<"Please enter a year later than 1582: ";
    cin>>x;
    
        if(x>1582){
            for (int i = 0; i<12; i++) {
                getFirst(i, 1, x);
                printMonth(i, d, x);
                            }
        }
        else{
            cout<<"That year is before 1582, please enter a better year: ";
            cin>>x;
        }
    
      return 0;
}

int getFirst(int month, int day, int year){

    a = (14-month)/12;
 	y = year-a;
	m = month+12*a-2; 
	d = (day+y+y/4-y/100+y/400+(31*m/12))%7; 
    
    return d;
}

void printMonth(int month, int startDay, int year){
    
    int daysInMonths[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    int copy = 1;
    int counter = 1;
    
   
    cout<<MONTHARRAY[month]<<endl;
    
    if(year%4 == 0)
       daysInMonths[1] = 29;
    
        for(int i = 0; i <=6; i++){
        cout<<DAYARRAY[i];
        
        if(i == 6)
            cout<<endl;
    }
    
        while(copy<=daysInMonths[month]){
        
        
        if(counter <= startDay)
            cout<<left<<setw(5)<<" ";
        
                else{
            cout<<left <<setw(5) <<copy;
            copy++;
        }
        
        
        if(counter%7 == 0 )
            cout<<endl;
        
        counter++;
    }
    
    cout<<endl;
}


Any help would be much appreciated.
A few things. In getFirst(...) you return d, but you don't use it. That essentially makes all of your math worthless. I believe you may have intended to use pass-by-reference, or maybe you meant to assign something to the returned value. This might give you the correct answer.

I also have no idea what's going on in the getFirst function. Print month is also wrong since February could possibly have 29 days. Like I said, I have no idea what's going on so I can't exactly decode your program, hope my little bit of help has actually helped you.
The getFirst function is an algorithm my instructor gave us to find what day of the week a given date falls on given the date, year, and month. I just passed a 1 in so I could find the day of the week the first falls on.

In the printMonth function I account for the possibility of a leap year with this if statement:
1
2
 if(year%4 == 0)
       daysInMonths[1] = 29;


I think my issue has something to do with the code within and after the while loop.
Looking over everything, your startDay is calculated incorrectly. You might need to recheck the equation your teacher gave you. I added parenthesis to show what's going on. I know the equation is wrong.
1
2
3
4
5
6
7
8
9
int getFirst(int month, int day, int year) {

   a = (14 - month) / 12;
   y = year - a;
   m = month + 12 * a - 2;
   d = (day + y + (y / 4) - (y / 100) + (y / 400) + (31 * m / 12)) % 7;

   return d;
}


Also, you if statement for February is incorrect as well. It should be:
if((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))

We don't have leap years on the 100th years, but we do on the 400th's. Just trying to make sure your code is correct ;)
Thank you for that leap year bit. The equation is the same as the one my teacher gave me and it worked properly in a simpler program we wrote at the beginning of the year, so I'm a little bit confused about the issue here.
I got it. The algorithm was designed to only take input of numbers larger than 0 for the month variable and I had my month array set up so that MONTHARRAY[0] = January. The program works fine now. Thank you for your help.
Topic archived. No new replies allowed.