Home > Mobile >  How to delete spacing after row is complete in c ?
How to delete spacing after row is complete in c ?

Time:02-07

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 I am noticing an extra space after the last row is completed. How do I fix this extra spacing issue? please help! I know the code to fix this should perhaps be somewhere in the void function, but I'm stuck on how to write it to delete that extra space or row that is created after the last day of the month. Here's an example picture of the output when a user inputs April 2050 for example... As you can see.. there's a space at the bottom... and this only seems to happen when the last row is full and no date is carried over.

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)));
}

/**
 * Checks through months January and February to find the starting day of
 *the month following the Gregorian calendar.
 * @returns extra day and an extra day if 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 << " ";
         }
         if (day != days_per)
         {
          std::cout << " ";
         }
      }
   }
//I believe the error can be fixed somewhere here?
}

// 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();
   std::cout << std::endl;
   return 0;
}

CodePudding user response:

I think this will fix it:

  • That's the block dealing with the last day of the week (Saturday).
  • Print it always.
  • If it is the last day of the month, don't do anything else.
  • Otherwise: 1) print a newline, and 2) if the next day has 1 digit (current day < 9), print a space.

[Demo]

      if (  count > 6)
      {
         count = 0;
         std::cout << day;
         if (day != days_per)
         {
            std::cout << '\n';
            if (day < 9)
            {
                std::cout << " ";
            }
         }
      }

CodePudding user response:

you dont need to delete anything, just dont produce it. Your probelm is here

         if (day >= 9)
        {
            std::cout << day << '\n';
        }
        else
        {
            std::cout << day << '\n' << " ";
        }

when you come to the end of a line you do a line feed if even it was the last day.

change to

     if (day >= 9)
     {
        std::cout << day;
        if (day != days_per) std::cout << '\n';
     }
     else
     {
        std::cout << day << '\n' << " ";
     }

you dont need it on the other path because thats never hit at the end of the month (day is alwasy 2 digits)

  •  Tags:  
  • Related