Calendar Program

I'm trying to write a Calendar program, but I'm having trouble. It asks the user for the number of days in a given month and then the offset (from Monday), and then it will display it to the screen. I'm just trying to get the basic looks of it down before I get into the more specifics like the leap years and so forth. Here's how I'm hoping it will look:

Su Mo Tu We Th Fr Sa
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

However, I can only get it to loop all the way to the right so that it looks like this:

Su Mo Tu We Th Fr Sa
1 2 3 4 5 6 7 8 9 10 11 12 13 14...

My question is, how do I shape it like the above so that each of the rows only extend until the end of the dates? Also, how can I do the offset so that my dates will shift and start depending on the user input? Here is my code so far.

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
#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()
{
   //Get the number of days
   int numDays = getNumDays();

   //Get the day offset
   int offset = getOffset();

   //Displays the result
   displayTable(numDays, offset);

   return 0;
}


/**********************************************************************
 * DISPLAYTABLE
 * Displays the physical table to the screen.
 ***********************************************************************/
void displayTable(int numDays, int offset)
{
   //Declare Variables
   int days;
   int test;

   //List the day headings
   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";

   //Displays number of days
   for (days = 1; days <= (numDays); days++)
      cout << "  " << setw(2) << days;
   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;
}

In the for loop in your displayTable(), you can check if days divides perfectly by 7 (a % b == 0), and if it does cout a newline. Can you figure out how to do that?

-Albatross
I'm still struggling a little bit. I can get it to either display all of the numbers in a long string (like shown above) or show absolutely nothing except one number. Where does the "if" statement need to be placed?

Also, I am completely CLUELESS on how to do the offset. Can I do another sort of loop that adds blank placeholders or something like that based on the number that the user inputs?
The if block would go inside your for loop after the cout (you will need {} for that).

As for the offset... I recall there being a standard library set of functions for that... but printing whitespaces works too for of-setting.

-Albatross
Man, I don't know what I'm doing wrong. Am I missing a major pitfall? I just can't see what I'm doing wrong and it's still not working. I've been staring at this stupid block of code for hours.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
/**********************************************************************
 * DISPLAYTABLE
 * Displays the physical table to the screen.
 ***********************************************************************/
void displayTable(int numDays, int offset)
{
   //Declare Variables
   int days;

   //List the day headings
   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";

   //Displays number of days
   for (days = 1; days <= numDays; days++)
      cout << "  " << setw(2) << days;
   {
   if (days % 7 == 0)
      cout << "\n" << days;
   }
   return;
}
The { } around your for loop are slightly misplaced. As it is:

1
2
3
4
5
6
for (days = 1; days <= numDays; days++)
      cout << "  " << setw(2) << days; //for loop only loops this line
   {//random scope entry
   if (days % 7 == 0)
      cout << "\n" << days;
   }

Thank you guys SO much! I'm ALMOST there. I have on last tiny little problem that I can't quite figure out.

Here is my function so far:
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
void displayTable(int numDays, int offset)
{
   //Declare Variables
   int days;
   int offsetCalc;

   //Calculations
   offsetCalc = offset + (7 - offset);

   //List the day headings
   cout << "  Su  Mo  Tu  We  Th  Fr  Sa\n";

   //Offset
   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)
      ;

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

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


My last little problem is that I want the first row of days to wrap around if it runs past Saturday. So I created a little bit of code:

1
2
3
   //Offset
   if (offsetCalc == 7)
      cout << "\n";


This will insert a new line if it reaches the end of the row. However, if I place it before my FOR loop, then it doesn't get read and only inserts a blank line, which does me nothing. If I place it after my FOR loop, it does the same thing and just inserts a new blank line. And then, if I place it inside my FOR loop, it just gets repeated and repeated over and over and inserts a newline after each number is written to the screen.

How can I make it so that it will only be executed on the first row and then disregarded afterwards? Thank you guys!
Bump. Any suggestions?
Topic archived. No new replies allowed.