Calendar Project (easy fix)

How do I get this code to work in a way that looks like a calendar. I have tried but it doesnt look right. The first week is always off i need help plz.
It displays moving the program to the right too much. When offset is put.
It has the appearance of a calendar and just has one tiny error
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
#include <iostream>
#include <iomanip>
using namespace std;

void displayTable(int numDays, int offset);
int getNumDays();
int getOffset();


/**********************************************************************
 * MAIN
 * Pretty much a delegator. Calls other functions.
 ***********************************************************************/
int main()
{
   int numDays = getNumDays();
   int offset = getOffset();

   displayTable(numDays, offset);
   cout <<"\n";
   return 0;
}


/**********************************************************************
 * DISPLAYTABLE
 * Displays the physical table to the screen.
 ***********************************************************************/
void displayTable(int numDays, int offset)
{

   int days;
   int offsetCalc;


   offsetCalc = (7 - offset);


   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";


   if (offset == 0)
      cout << setw(4)  << " ";
   if (offset == 1)
      cout << setw(8)  << " ";
   if (offset == 2)
      cout << setw(12) << " ";
   if (offset == 3)
      cout << setw(16) << " ";
   if (offset == 4)
      cout << setw(20) << " ";
   if (offset == 5)
      cout << setw(24) << " ";
   else (offset == 6) ;


   if (offsetCalc == 7)
      cout << "\n";


   for (days = 1; days <= numDays; days++)
   {
      cout << "  " << setw(2) << days;
      if (days % 7 == 0)
         cout << "\n";
   }
   return;
}


/**********************************************************************
 * OFFSET
 * Determines the number of days to offset the Calendar and then returns
 * that value back to displayTable.
 ***********************************************************************/
int getOffset()
{
   int offset;
   cout << "Offset: ";
   cin >> offset;
   return offset;
}


/**********************************************************************
 * NUMDAYS
 * Accepts the number of days from the user.
 ***********************************************************************/
int getNumDays()
{
   int numDays;
   cout << "Number of days: ";
   cin >> numDays;
   return numDays;
}
Last edited on
@dagurr

Here's your calendar program. Just a few very small errors, that were easy to correct. I commented the code to show what I did.

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

void displayTable(int numDays, int offset);
int getNumDays();
int getOffset();


/**********************************************************************
* MAIN
* Pretty much a delegator. Calls other functions.
***********************************************************************/
int main()
{
	int numDays = getNumDays();
	int offset = getOffset();

	displayTable(numDays, offset);
	cout << "\n";
	return 0;
}

/**********************************************************************
* DISPLAYTABLE
* Displays the physical table to the screen.
***********************************************************************/
void displayTable(int numDays, int offset)
{

	int days;
	int offsetCalc;


	offsetCalc = (7 - offset);

	// Using a for loop instead of all the if statements
	cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";
	for (int y = 0; y < offset; y++)
		cout << "    ";

	/*if (offset == 0)
		cout << setw(4) << " ";
	if (offset == 1)
		cout << setw(8) << " ";
	if (offset == 2)
		cout << setw(12) << " ";
	if (offset == 3)
		cout << setw(16) << " ";
	if (offset == 4)
		cout << setw(20) << " ";
	if (offset == 5)
		cout << setw(24) << " ";
	else (offset == 6);


	if (offsetCalc == 7)
		cout << "\n";
		*/

	for (days = 0; days < numDays; days++)
	{
		cout << "  ";
		if (days + 1 < 10)
			cout << " "; // To keep numbers less than 10, aligned
		cout << days + 1;
		if ((offset+days+1) % 7 == 0)// Have to add in the offset
			cout << "\n";
	}
	return;
}

/**********************************************************************
* OFFSET
* Determines the number of days to offset the Calendar and then returns
* that value back to displayTable.
***********************************************************************/
int getOffset()
{
	int offset;
	cout << "Offset: ";
	cin >> offset;
	return offset;
}

/**********************************************************************
* NUMDAYS
* Accepts the number of days from the user.
***********************************************************************/
int getNumDays()
{
	int numDays;
	cout << "Number of days: ";
	cin >> numDays;
	return numDays;
}
Thanks for your help I spent hours and now I can understand where I went wrong.
Topic archived. No new replies allowed.