1

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 project, but I am noticing an extra space after the last number when the calendar is finished. How do I fix this extra spacing issue?

//extracted...
         if (day >= 9)
         {
            std::cout << day;
         }
         else 
          std::cout << day << " ";
          std::cout << " ";
      }
   }

  • 1
    I have old version of printing calendar I made long while back that is in C, do you want to see? – Asphodel Feb 06 '22 at 21:01
  • Sure, I would love to see it! @Asphodel –  Feb 06 '22 at 21:05
  • It was a whole program dedicated to printing the calendar according to the month and year user inputed, and it could also print current month. I'll show specifically the function that prints the calendar part, unless you want to see more. – Asphodel Feb 06 '22 at 21:08
  • That would be wonderful! –  Feb 06 '22 at 21:09
  • The very last `std::cout << " ";` statement in `printMonth` (the one that is misleadingly indented under `else`, but is in fact executed unconditionally) always prints a trailing space after every number, including the last one. – Igor Tandetnik Feb 06 '22 at 21:12
  • Hmm, I tried deleting it, but the whole code becomes discombobulated @IgorTandetnik –  Feb 06 '22 at 21:21
  • Well, you need to print that space between days. Just not after the last one. – Igor Tandetnik Feb 06 '22 at 21:28
  • Don't edit the question in a way which invalidate existing answers. – Jarod42 Feb 09 '22 at 08:50

3 Answers3

0

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");
}
Asphodel
  • 180
  • 1
  • 1
  • 11
0

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.

Schol-R-LEA
  • 436
  • 4
  • 9
  • then `std::cout << day << " ";` should do the job. But it introduce other issue: now for `day >= 9`, there is no separator between numbers. main problem in OP code is to handle the writing of the separation correctly. – Jarod42 Feb 09 '22 at 08:55
0

std::cout << std::setw(2) << day might get rid of if (day >= 9) stuff.

Then to write number with some separator, I tend to use following pattern

const char* sep = "";
for (auto e : some_list) {
    std::cout << sep << e;
    sep = ", "; // or any separator
}

Resulting into

const char* sep = "";
for (day = 1; day <= days_per; day++)
{
    std::cout << sep << std::setw(2) << day;
    sep = " ";
    if (++count > 6)
    {
        std::cout << '\n';
        count = 0;
        sep = "";
    }
}

Demo

Jarod42
  • 203,559
  • 14
  • 181
  • 302