Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

There is an Alignment Issue and Extra Spacing in my code, but I cannot figure out where?

I have been working on a project. I am limited to using a few libraries so any additional library would not be helpful. I am building a calendar and so far my calendar works, but each time – after the number becomes double digits on the second row – there is a shift
and my calendar doesn’t align neatly on top of the next row. I can not figure out where the spacing issue allows this to happen. Also, I am noticing an extra space after the last number when the calendar is finished. How do I fix this alignment issue and the extra spacing issue? please help! Here’s an example picture of the output.
UPDATE: Thanks Drew for helping me with the alignment! However, I now face the issue of one extra spacing at the end. How do I fix that?
enter image description here

#include <iostream>

int month, day, year, start = 0;

#define MONTHS_PER_YEAR 12

const unsigned short DAYS_PER_MONTH[MONTHS_PER_YEAR] =
{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

const char MONTH_NAMES[MONTHS_PER_YEAR][10] =
{"January",   "February", "March",    "April",
"May",       "June",     "July",     "August",
"September", "October",  "November", "December"};

/**
 * Returns true if indicated year is a leap year.
 * @param year: the year that user inputs.
 * @return true if year is a leap year, and false otherwise.
 */
bool leapYear(int year)
{
    return (((year % 400) == 0) || ((year % 4 == 0) && !(year % 100 == 0)));
}

/**
 * Iterates through months Janauray and February to find the starting day of
 *the month following the Gregorian caledner.
 * @returns extra day for leap year or not.
 */
int day_of()
{
   int subset_days[] = {0, 3, 2, 5, 0, 3, 5, 1, 4, 6, 2, 4};
   if (month < 3)
   {
      year--;
   }
   return ((year + year / 4 - year / 100 + year / 400 + subset_days[month - 1] + day) % 7);
}

/**
 * Prints the calender and calculates how many days are in month and which months
 * day starts on.
 */
void printMonth()
{
   start = day_of();
   int count = 0;
   int days_per = DAYS_PER_MONTH[month - 1];
   if (leapYear(year + 1) && month == 2)
   {
     days_per = DAYS_PER_MONTH[month - 1] + 1;
   }
   if (start == 6)
   {
      start = -1;
      std::cout << " ";
   }
   for (count = 0; count <= start; count++)
   {
      if (count > 0)
      {
         std::cout << "   ";
      }
      else
      {
         std::cout << "    ";
      }
   }
   for (day = 1; day <= days_per; day++)
   {
      if (++count > 6)
      {
         count = 0;
         if (day > 9)
         {
            std::cout << day << '\n';
         }
         else
         {
            std::cout << day << '\n' << " ";
         }
      }
      else
      {
         if (day > 9)
         {
            std::cout << day;
         }
         else 
            std::cout << day << " ";
            std::cout << " ";
      }
   }
   std::cout << std::endl;
}

// Controls operation of the program.
int main()
{
   std::cout << "Enter the month: ";
   std::cin >> month;
   std::cout << "Enter the year: ";
   std::cin >> year;
   std::cout << MONTH_NAMES[month - 1] << " " << year << std::endl;
   std::cout << "Su" << "  "<< "M" << "  "<< "T"<< "  " << "W" << " " << "Th" << "  " << "F" << " " << "Sa\n";
   printMonth();
   return 0;
}

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Change every (day > 9) to (day >= 9 ).

You are using that condition to decide how much whitespace should appear after the number.

Demo

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading