title: Offset for a calendar program
body: <p>This program takes any user input year since 1753 and month and creates a calendar for it. However I'm having issues with the offset which is the day the month starts out on. As far as I can tell it is just the offset that is off and everything else seems to work great.
  Here is my code.</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  using namespace std;
  
  
  int getMonth(int month);
  int getYear(int year);
  int computeOffset(int year, int month);
  int numDaysYear(int year);
  int numDaysMonth(int year, int month);
  bool isLeapYear(int year);
  void display(int year, int month, int offset);
  
  
  /**********************************************************************
   * This function will call all the functions necessary to make a calendar
   * for any given month and year.
   ***********************************************************************/
  int main()
  {
     int numDays;
     int offset;
     int month;
     int year;
  
     month = getMonth(month);
  
     year = getYear(year);
  
     offset = computeOffset(year, month);
  
     display(year, month, offset);
  
     return 0;
  }
  
  /***********************************************************************
   * Gets the month number.
   **********************************************************************/
  int getMonth(int month)
  {
     cout &lt;&lt; "Enter a month number: ";
     cin &gt;&gt; month;
  
     while ( month &lt; 1 || month &gt; 12)
     {
        cout &lt;&lt; "Month must be between 1 and 12.\n"
             &lt;&lt; "Enter a month number: ";
        cin &gt;&gt; month;
     }
  
     return month;
  }
  
  /***********************************************************************
   * Gets the year.
   **********************************************************************/
  int getYear(int year)
  {
     cout &lt;&lt; "Enter year: ";
     cin &gt;&gt; year;
  
     while ( year &lt; 1753)
     {
        cout &lt;&lt; "Year must be 1753 or later.\n"
             &lt;&lt; "Enter year: ";
        cin &gt;&gt; year;
     }
     return year;
  }
  
  /***********************************************************************
   * Computes the offset.
   **********************************************************************/
  
  int computeOffset(int year, int month)
  {
     int offset = 0;
     int count = year - 1753;
     for ( int iYear = 0; iYear &lt; count; iYear++)
     {
        offset = ( offset + 365 + isLeapYear(year)) % 7;
     }
  
     for ( int iMonth = 1; iMonth &lt; month; iMonth++)
     {
        offset = ( offset + numDaysMonth(year, iMonth)) % 7;
     }
  
     return offset;
  }
  
  
  
  /***********************************************************************
   * Computes the number of days in the given year.
   **********************************************************************/
  int numDaysYear(int year)
  {
     int daysYear = 365 + isLeapYear(year);
     return daysYear;
  }
  
  /***********************************************************************
   * Calculates the number of days in the given month.
   **********************************************************************/
  int numDaysMonth(int year, int month)
  {
     int daysMonth;
  
     if ( month == 1)
        daysMonth = 31;
     else if ( month == 2)
     {
        if (isLeapYear(year) == true)
           daysMonth = 29;
        else
           daysMonth = 28;
     }
     else if ( month == 3)
        daysMonth = 31;
     else if ( month == 4)
        daysMonth = 30;
     else if ( month == 5)
        daysMonth = 31;
     else if ( month == 6)
        daysMonth = 30;
     else if ( month == 7)
        daysMonth = 31;
     else if ( month == 8)
        daysMonth = 31;
     else if ( month == 9)
        daysMonth = 30;
     else if ( month == 10)
        daysMonth = 31;
     else if ( month == 11)
        daysMonth = 30;
     else if ( month == 12)
        daysMonth = 31;
  
     return daysMonth;
  }
  
  /***********************************************************************
   * Determines if given year is a leap year.
   **********************************************************************/
  bool isLeapYear(int year)
  {
     if ( year % 4 == 0 &amp;&amp; year % 100 != 0 || year % 400 == 0)
        return true;
     else
        return false;
  }
   /**********************************************************************
   * Displays the calender table.
   **********************************************************************/
  void display(int year, int month, int offset)
   {
      int dayOfWeek;
      int day;
  
      cout &lt;&lt; endl;
      if ( month == 1)
         cout &lt;&lt; "January";
      else if ( month == 2)
         cout &lt;&lt; "February";
      else if ( month == 3)
         cout &lt;&lt; "March";
      else if ( month == 4)
         cout &lt;&lt; "April";
      else if ( month == 5)
         cout &lt;&lt; "May";
      else if ( month == 6)
         cout &lt;&lt; "June";
      else if ( month == 7)
         cout &lt;&lt; "July";
      else if ( month == 8)
         cout &lt;&lt; "August";
      else if ( month == 9)
         cout &lt;&lt; "September";
      else if ( month == 10)
         cout &lt;&lt; "October";
      else if ( month == 11)
         cout &lt;&lt; "November";
      else if ( month == 12)
         cout &lt;&lt; "December";
  
  
      cout &lt;&lt; ", " &lt;&lt; year &lt;&lt; "\n";
      // Display month header
      cout &lt;&lt; "  Su  Mo  Tu  We  Th  Fr  Sa\n";
  
      // Gets the correct offset width and end the line on the right
      //day of the week
      if (offset == 0)
      {
         day = 2;
         cout &lt;&lt; setw(6);
      }
      else if (offset == 1)
      {
         day = 3;
         cout &lt;&lt; setw(10);
      }
      else if (offset == 2)
      {
         day = 4;
         cout &lt;&lt; setw(14);
      }
      else if (offset == 3)
      {
         day = 5;
         cout &lt;&lt; setw(18);
      }
      else if (offset == 4)
      {
         day = 6;
         cout &lt;&lt; setw(22);
      }
      else if (offset == 5)
      {
         day = 7;
         cout &lt;&lt; setw(26);
      }
      else if (offset == 6)
      {
         day = 1;
         cout &lt;&lt; setw(2);
      }
      else
         cout &lt;&lt; "Error offset must be &gt;= 0 and &lt;=6\n";
  
      // The loop for displaying the days and ending the line in the right place
      for ( dayOfWeek = 1; dayOfWeek &lt;= numDaysMonth(year, month); dayOfWeek++ )
      {
         cout &lt;&lt; "  " &lt;&lt;  setw(2) &lt;&lt; dayOfWeek;
         ++day;
         if (day == 8)
         {
            cout &lt;&lt; "\n";
            day = 1;
         }
      }
      if ( day &gt;= 2 &amp;&amp; day &lt;= 7)
         cout &lt;&lt; "\n";
  
      return;
   }`
  </code></pre>