Need help with calendar

this is my output for the first 3 month. For some reason this website moved all numbers to the left and I cant show exactly how my calendar looks like. The 1 is a Fri in every month. I have no Idea how to start each next month from the diferent day, cant figure out the formula. After the calendar is my code. If the first day of the year is on Friday, then every month start on Fri. Guys, please help.
January, 2016
------------------------------------------
Sun Mon Tue Wed Thu Fri Sat
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, 2016
------------------------------------------
Sun Mon Tue Wed Thu Fri Sat
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

March, 2016
------------------------------------------
Sun Mon Tue Wed Thu Fri Sat
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

#include<iostream>
#include<iomanip>
#include<string>
using namespace std;
void monthName(int,int);
int daysinMonth(int,int);

int main()
{
int year, day;
cout << "Please, enter the year and the first day of the year: 0 for Sunday, 1 for Monday... 6 for Saturday." << endl;
cin >> year >> day;
int m = 8;
int newNum = 8 - day;
for (int v = 1; v < 13; v++)
{
monthName(year, v);
for (int j = 0; j < day * 6; j++)
cout << " ";
for (int jk = 1; jk <= 7 - day; jk++)
cout << setw(6) << jk;
cout << endl;
while (newNum <= daysinMonth(v,year))
{
cout << setw(6) << newNum;
if (newNum - (8 - day) == 6 || newNum - (8 - day) == 13 || newNum - (8 - day) == 20 || newNum - (8 - day) == 27)
cout << endl;
newNum++;
}

cout << endl << endl;
newNum = 8-day;
}

return 0;
}

void monthName(int year, int i)
{
string month;
switch (i)
{
case 1:
month = "January";
break;
case 2:
month = "February";
break;
case 3:
month = "March";
break;
case 4:
month = "April";
break;
case 5:
month = "May";
break;
case 6:
month = "June";
break;
case 7:
month = "July";
break;
case 8:
month = "August";
break;
case 9:
month = "September";
break;
case 10:
month = "October";
break;
case 11:
month = "November";
break;
case 12:
month = "December";
break;
}
cout << "\t\t"<< month<<", " << year << endl;
cout << setfill('-') << setw(42) << '-' <<setfill(' ')<< endl;
cout << setw(6) << "Sun" << setw(6) << "Mon" << setw(6) << "Tue" << setw(6) << "Wed" << setw(6) << "Thu" << setw(6) << "Fri" << setw(6) << "Sat" << endl;
}

int daysinMonth(int a, int y)
{
int p;
if (a == 1 || a == 3 || a == 5 || a == 7 || a == 8 || a == 10 || a == 12)
p = 1;
else if (a == 4 || a == 6 || a == 9 || a == 11)
p = 2;
else if (a == 2 && (y % 400 == 0 || (y % 4 == 0 && y % 100 != 0)))
p = 3;
else
p = 4;
switch(p)
{
case 1:
return 31;
case 2:
return 30;
case 3:
return 29;
case 4:
return 28;
}
}
Last edited on
Well to start, your Cal is not correct for Feb or March.
The last day of Jan is Sunday, so the next day should be Monday.

To line up your output you first need to know what column to use.
It may not format right but below you can see I added a comment line to show the position of the middle of the day of the week.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <iomanip>      // std::setw
using namespace std;

void Function1()
	{
	//       123456789012345678901234567890
	//        2   6   0   4   8   2   6
	cout << "Sun Mon Tue Wed Thu Fri Sat" << endl;
	std::cout << std::setw(22) << "1";
	std::cout << std::setw(4) << "2";
	std::cout << endl;
	}

int main()
{
	Function1();
}


Output should look like
1
2
Sun Mon Tue Wed Thu Fri Sat
                     1   2


You could use a if statement
1
2
if (first day of month = Friday)
std::cout << std::setw(22);
Last edited on
Oh, no, Samuel, my numbers are perfectly lined up. You can run my code and you will see. I just cant copy and paste my output correctly as you doing in the purple square.
Last edited on
How to use tags:

http://www.cplusplus.com/articles/z13hAqkS/


Please edit your original post so it uses code tags.

With your code, it's easier to have an array of the month names, and another array of the days in each month - then you can use the array subscript to return the correct string / value. This is better than a long switch or lots of logic.
thanks, will try.
Topic archived. No new replies allowed.