Hi,
This is what I have done so far (below)
I'm having a problem with the spacing as shown in the output (below the code)
Can anyone please help me fix the spacing?

Thank You!!

************************************************** **************
CODE:

#include <iostream>
using namespace std;

void question (int input);
void calendar (int year, int start_day);
void line_num (int count_days, int days_month);
bool leap_year (int year);
void nums_month (int count, int& days_month, int& prev_day);
void month_names (int count);
void day_names ();
void spacing (int start_day, int& count_col, int count, int& prev_spacing);


int input;
int start_day;
int days_month;
int year;
int prev_day;
int count_days = 1;
int count_col = 0;
int prev_spacing;

int main()
{
question(input);
calendar(year, start_day);
}

void question (int input)
{
cout << "Which year of the calendar do you want?" << endl;
cin >> year;
cout << "Which day do you want January 1st to start on?" << endl << "Ex: Sunday = 0, Monday = 1, Tuesday = 2, etc" << endl;
cin >> start_day;
}

void calendar (int year, int start_day)
{
int count;
for (count = 1; count <= 12; count++)
{
month_names (count);
day_names ();
nums_month(count, days_month, prev_day);
spacing (start_day, count_col, count, prev_spacing);
line_num (count_days, days_month);
}
}

void line_num (int count_days, int days_month)
{
while (count_days <= days_month)
{
if (count_col == 7)
{
cout << endl;
count_col = 0;
}
cout << " " << count_days << " ";
count_col++;
count_days++;
}
count_col = 0;
cout << endl << endl;
}

bool leap_year (int year)
{
if (year % 400 == 0)
{
return true;
}

if (year % 100 == 0)
{
return false;
}

if (year % 4 == 0)
{
return true;
}
return false;
}

void nums_month (int count, int& days_month, int& prev_day)
{
switch (count)
{
case 1:
days_month = 31;
break;
case 2:
if (leap_year(year))
days_month = 29;
if (!leap_year(year))
days_month = 28;
prev_day = 31;
break;
case 3:
days_month = 31;

if (leap_year(year))
prev_day = 29;
if (!leap_year(year))
prev_day = 28;
break;
case 4:
days_month = 30;
prev_day = 31;
break;
case 5:
days_month = 31;
prev_day = 30;
break;
case 6:
days_month = 30;
prev_day = 31;
break;
case 7:
days_month = 31;
prev_day = 30;
break;
case 8:
days_month = 31;
prev_day = 31;
break;
case 9:
days_month = 30;
prev_day = 31;
break;
case 10:
days_month = 31;
prev_day = 30;
break;
case 11:
days_month = 30;
prev_day = 31;
break;
case 12:
days_month = 31;
prev_day = 30;
break;
}
}


void month_names (int count)
{
switch (count)
{
case 1:
cout << " January" << endl;
break;
case 2:
cout << " February" << endl;
break;
case 3:
cout << " March" << endl;
break;
case 4:
cout << " April" << endl;
break;
case 5:
cout << " May" << endl;
break;
case 6:
cout << " June" << endl;
break;
case 7:
cout << " July" << endl;
break;
case 8:
cout << " August" << endl;
break;
case 9:
cout << " September" << endl;
break;
case 10:
cout << " October" << endl;
break;
case 11:
cout << " November" << endl;
break;
case 12:
cout << " December" << endl;
break;
}
}

void day_names ()
{
cout << " S M TU W TH F S " << endl;
}

void spacing (int start_day, int& count_col, int count, int& prev_spacing)
{
if (count == 1)
{
int count1;

for (count1 = 0; count1 < start_day; count1++)
{
cout << " " << endl;
prev_spacing++;
}
count_col =+ start_day;
}
else if (count != 1)
{
int spacing;
int count2;
int count_month = count;
nums_month (count_month, days_month, prev_day);

spacing = (prev_spacing + prev_day) % 7;
prev_spacing = 0;

for (count2 = 0; count2 < spacing; count2++)
{
cout << " ";
prev_spacing++;
count_col =+ spacing;
}
}
}

************************************************** **************
OUTPUT:

Which year of the calendar do you want?
2005
Which day do you want January 1st to start on?
Ex: Sunday = 0, Monday = 1, Tuesday = 2, etc
1
January
S M TU W TH 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
S M TU W TH 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
S M TU W TH 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
S M TU W TH 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

May
S M TU W TH 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

June
S M TU W TH 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

July
S M TU W TH 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

August
S M TU W TH 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

September
S M TU W TH 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

October
S M TU W TH 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

November
S M TU W TH 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

December
S M TU W TH 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

Press any key to continue . . .

Recommended Answers

All 6 Replies

Member Avatar for r.stiltskin

You can use the setw() manipulator to set the width of the field occupied by each value.

Each instance of setw() works only for a single output value, so if you have several values to output and you want to specify the field width for each one, you have to use setw() before EVERY output item. Put the desired field width as the argument to setw(). Here's an example, using a field width of 7:

#include <iostream>
#include <iomanip>
using namespace std;

int main () {
    cout << setw( 7 ) << 1 << setw(7) << 2 << setw(7) << 3 << setw(7) << 4 << endl;
}

Note also that you have to #include the <iomanip> header.

I got the spacing part but I still couldn't get the numbers to line up with the correct day of the week. For example: If the last day of January ends on Wednesday then the next month (February) starting day should start on Thursday.
How would I do this?

Thank You!!

Create an int variable called currentDayOfWeek and initialize it to the appropriate value for the first day of the calendar. Every time you create a new day advance currentDayOfWeek by one and when currentDayOfWeek is the last of the week, reset back to first day of the week. Keep track of the currentDayOfWeek for the last day of the preceding month. The current month will start on currentDayOfWeek for the last day of the preceding month plus 1.

Hi,I'm not getting where to add this currentDayOfWeek.
Do you think you can help me add it to my program like maybe at the portion where it should be?
That would nice if you can

Thank You!!

Where do you output the first day of the month?
Do you know what day of the week this first day is?
What can you do to move that 1st day under the correct day label before you output the first day?

In line 33 you ask which day of the week Jan 1 is to be and you store that value as an int. It's called start_day. You can use that instead of currentDayOfWeek as it is used the same way I said to use currentDayOfWeek.

In line_num() use start_day instead of count_col to keep track of what day of the week the current day will be displayed under. That way it should stay on track as you go from month to month if you get rid of line 63.

Although I believe the above will get you through the theory of how to keep track of the day of the week each day is, your code has a number of problems that will need to be corrected, some of which may prevent adequate implementation of the above strategy. For example: your understanding of variable scope needs to be improved. Your use of functions and variables with the same name is asking for trouble. You need to be careful which variables need to be reset to what value. There are other concerns that I have about your code, too, but they shouldn't affect the ability to get the basic task done like the above examples might.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.