Calendar Project (easy fix) (Slight error)

This works perfectly the only problem is the offset 6 is wrong and displays it not like it should. check it out and what do i need to do to fix this.

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

/**********************************************************************
 * This function does will first ask the user to put a number
 * ask the user
 ***********************************************************************/
void displayTable(int numDays, int offset);
int getNumDays();
int getOffset();


/**********************************************************************
 * Main calls all the functions.
  ***********************************************************************/
int main()
{
   int numDays = (getNumDays()) + 1;
   int offset = getOffset();

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

/**********************************************************************
 * Displays the table.
 ***********************************************************************/
void displayTable(int numDays, int offset)
{

   int dow = 1;

   if (offset == 6)
      offset = -1;
   
   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n        ";
   for (int count = 1; count < offset; count++)
   cout << "    ";
   dow++;
   
   for (int days = 1; days < numDays; days++)
   {
      cout << "  ";
   if (days < 10) //days are set to only have one space not 2
      cout << " ";
      cout << days;
   if ((offset + days + 1) % 7 == 0)
      cout << "\n";
   
   }
   return;
}

/**********************************************************************
 * Asks the user for the offset
 ***********************************************************************/
int getOffset()
{
   int offset;
   cout << "Offset: ";
   cin >> offset;
   return offset;
}

/**********************************************************************
 * Asks the user for the numbers of days to set.
 ***********************************************************************/
int getNumDays()
{
   int numDays;
   cout << "Number of days: ";
   cin >> numDays;
   return numDays;
}
Last edited on
closed account (D80DSL3A)
I found several problems in the displayTable function. I'm not sure what the lowest value for offset is supposed to be, but I went with 0 = no shift = 1st on Sunday.
The extra spaces following the \n line 38, the +1 line 49, the offset = -1 line 36 all contributed to the problem.
Here's a version which is working for all offset values of 0-6.
The lines commented out are what you had, so you can see what I changed.
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
void displayTable(int numDays, int offset)
{

   int dow = 1;// what's this for?

//   if (offset == 6)
 //     offset = -1;

//   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n        ";
   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";
//   for (int count = 1; count < offset; count++)
   for (int count = 0; count < offset; count++)
   cout << "    ";
   dow++;// not used anywhere

   for (int days = 1; days < numDays; days++)
   {
      cout << "  ";
   if (days < 10) //days are set to only have one space not 2
      cout << " ";
      cout << days;
//   if ((offset + days + 1) % 7 == 0)
   if ((offset + days) % 7 == 0)
      cout << "\n";

   }
   return;
}
Topic archived. No new replies allowed.