formatting C++ Calendar Program

Need help! I'm trying to figure out how to print the first day in the right day of the month

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

bool IsLeap (int);
int firstDay (int);
int daysInMonth(int, bool);
void month (int, int&);
void printHeader(int, int);
void skip (int);
void skipToDay(int);


int main () {
int y, m = 1;
int numDays, firstDayYear;
bool leap;
cout << "Please enter year to view the calendar: ";
cin >> y; cout << endl;
firstDayYear = firstDay(y);
cout << firstDayYear;
leap = IsLeap(y);
for(m = 1; m<=12; m++){
numDays = daysInMonth(m, leap);
printHeader(m, y);
month (numDays, firstDayYear);
cout << endl << endl;
}
cout << endl;
return 0;
}

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

int firstDay (int y) {
int janfirst;
y = y - 1;
janfirst = (1 + y + (y/4) - (y/100) + (y/400)) % 7;
return janfirst;
}

int daysInMonth (int m, bool leap){
switch(m){
case 1:
m = 31; break;
case 2:
if ( leap ){
m = 29; break;}
else
m = 28; break;
case 3:
m = 31; break;
case 4:
m = 30; break;
case 5:
m = 31; break;
case 6:
m = 30; break;
case 7:
m = 31; break;
case 8:
m = 31; break;
case 9:
m = 30; break;
case 10:
m = 31; break;
case 11:
m = 30; break;
case 12:
m = 31; break;
default:
break;
}
return m;
}

void printHeader (int m, int y){
cout << "**********************" << endl;
if (m == 1) {
cout << "* January " << y << " *" << endl;}
else if (m == 2) {
cout << "* February " << y << " *" << endl; }
else if (m == 3) {
cout << "* March " << y << " *" << endl; }
else if (m == 4) {
cout << "* April " << y << " *" << endl; }
else if (m == 5) {
cout << "* May " << y << " *" << endl; }
else if (m == 6) {
cout << "* June " << y << " *" <<endl; }
else if (m == 7) {
cout << "* July " << y << " *" << endl; }
else if (m == 8) {
cout << "* August " << y << " *" << endl; }
else if (m == 9) {
cout << "* September " << y << " *" << endl; }
else if (m == 10) {
cout << "* October " << y << " *" << endl; }
else if (m == 11) {
cout << "* November " << y << " *" << endl; }
else if (m == 12) {
cout << "* December " << y << " *" << endl; }

cout << "* S M T W T F S*" <<endl;
cout << "**********************" << endl;

}

void month (int numDays, int &firstDayYear) {
int day = 1;

for(day = 1; day <= numDays; day++){
cout << " " << setw(2) << day;
firstDayYear++;
if (firstDayYear > 6){ //something is missing in this code
cout << endl;
firstDayYear = 0;}
}
}

this is what I get

Please enter year to view the calendar: 2015

4**********************
* January 2015 *
* S M T W T F S*
**********************
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


**********************
* February 2015 *
* S M T W T F S*
**********************
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


**********************
* March 2015 *
* S M T W T F S*
**********************
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

**********************
* April 2015 *
* S M T W T F S*
**********************
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


Welcome to cplusplus.com :D
Please use code tags, so we can read your code better :)
Here it shows how: http://www.cplusplus.com/articles/jEywvCM9/

1
2
3
4
5
6
7
for(day = 1; day <= numDays; day++){
cout << " " << setw(2) << day;
firstDayYear++;
if (firstDayYear > 6){ //something is missing in this code
cout << endl;
firstDayYear = 0;}
}

I like that quote :b "something is missing in this code" :D

I'm trying to figure out how to print the first day in the right day of the month

Somewhere here you have to check the day of the week and skip that many days or print that many whitespaces :)
1
2
3
4
5
6
7
8
9
10
11
12
void month (int numDays, int &firstDayYear) {
    int day = 1;

    for(day = 1; day <= numDays; day++){
        cout << " " << setw(2) << day;
        firstDayYear++;
        if (firstDayYear > 6){ //something is missing in this code
            cout << endl;
            firstDayYear = 0;
        }
    }
}
Last edited on
yeah but I've tried everything but I don't how to put the first day on the right day :(
Topic archived. No new replies allowed.