I've been trying to get this stupid function to work for days and just cannot get it.

My display looks like this:

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

All I want it to do is have those last 4, 5, 6, and 7 wrap around to the next column. I've tried inserting "\n" different places, but cannot get it to work properly. Can anyone lend me any insight?

void displayTable(int numDays, int offset)
{
   //Declare Variables
   int days;
   int colCount = 0;

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

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

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

The first and last lines of the calendar are different from the others in that they can have empty spaces in them. The last line can be handled by stopping output for the line, but the first line needs the offset printed before the output starts. The number of days to print on the first line is 7 - offset. Then use a loop to print the offset and a second loop to days to print in the first line. Follow that by a third (and 4th, if you like) loop to print off the rest of the days, 7 days per line (with the last day not necessarily evenly divisible by 7) starting with the last number of last line plus one, and I think you should have it.

This article has been dead for over six months. Start a new discussion instead.