'User input Calendar in C

I am making a calendar that prompts the user to enter the number of days in the month and the starting day. It displays

Enter number of days in month: 31
Enter starting day of the week (1=Sun, 7=Sat): 1
  1  2  3  4  5  6
  7  8  9 10 11 12 13
 14 15 16 17 18 19 20
 21 22 23 24 25 26 27
 28 29 30 31

when it's supposed to display

Enter number of days in month: 31
Enter starting day of the week (1=Sun, 7=Sat): 1
      1  2  3  4  5  6  7
      8  9  10 11 12 13 14
     15 16 17 18 19 20 21
     22 23 24 25 26 27 28
     29 30 31

.

int main(void)
{
  int i, n, days, week;

  printf("Enter number of days in month: ");
  scanf("%d", &days);
  printf("Enter starting day of the week (1=Sun, 7=Sat): ");
  scanf("%d", &week);

  switch (week) {
    case 1: break;
    case 2: printf("   "); break;
    case 3: printf("      "); break;
    case 4: printf("         "); break;
    case 5: printf("            "); break;
    case 6: printf("               "); break;
    case 7: printf("                     "); break;
    default: printf("Error"); break;
    }
  
  for (i = 1; i <= days; i++) {
    if (week >= 8) {
      printf("\n");
      week = 0;
    }

    printf("%3d", i);

    week++;
  }
  

  return 0;
} 

the week is supposed to represent spaces in the calendar



Solution 1:[1]

This is the solution, Instead of doing:

for (i = 1; i <= days; i++) {
    if (week >= 8) {
      printf("\n");
      week = 0;
    }

Do:

for (i = 1; i <= days; i++) {
    if (week > 7) {
      printf("\n");
      week = 1;
    }

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 ChillZone