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 number when the calendar is finished. How do I fix this extra spacing issue? please help! Here's an example picture of the output. The "$" represents spacing in the output. I know the code to fix this should perhaps be somewhere in the void function, but I'm stuck on how to write it.
#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 << " ";
}
}
}
// 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:
Version in C: (it uses printf so maybe not what you want)
void printcalendarmonth(int month, int year)
{
int indent;
int nspace = 0;
Monthinfo m = getmonthinfo(month, year);
printf("\n%*s %d\n\n", 12, m.name, year); // ex.) January 2012
char weekTitle[] = " S M Tu W Th F S";
int spacing = sizeof(weekTitle)/7;
printf("%s\n", weekTitle);
indent = m.daystart * spacing; // m.daystart = weekday number first day is starting on: ex.) Sunday = 0 or 1 I forgot sorry lol
for (int i = 1; i <= m.days; i ) {
printf("%*.d", indent, i);
nspace = indent;
indent = spacing;
if (nspace >= spacing*7) {
printf("\n");
nspace = 0;
}
}
printf("\n");
}
CodePudding user response:
In the section
else
{
if (day >= 9)
{
std::cout << day;
}
else
std::cout << day << " ";
std::cout << " ";
}
You seem to have accidentally omitted the braces around the final else clause. It probably should read:
else
{
if (day >= 9)
{
std::cout << day;
}
else
{
std::cout << day << " ";
std::cout << " ";
}
}
This ensures that the dangling std::cout << " "; mentioned by @Igor-Tandetnik is part of the else clause, as you seem to have intended.

