TheUnknownChop
TheUnknownChop

Reputation: 117

Issue with printf - not printing put integer value

I am trying to assemble my C calendar program to make it looks like the real Unix program. Specifically, I am trying to make the first 3 months displaying like this:

                            2017
      January               February               March          
Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  
 1  2  3  4  5  6  7            1  2  3  4            1  2  3  4  
 8  9 10 11 12 13 14   5  6  7  8  9 10 11   5  6  7  8  9 10 11  
15 16 17 18 19 20 21  12 13 14 15 16 17 18  12 13 14 15 16 17 18  
22 23 24 25 26 27 28  19 20 21 22 23 24 25  19 20 21 22 23 24 25  
29 30 31              26 27 28              26 27 28 29 30 31     

Here is my code, it's a draft and I will try to reduce it later. I am doing the printing line-by-line method

void printJanFebMar(int year)
{
    printf("                               %d\n",year);
    //print months from January to March
    printf("      January               February               March\n");
    for(int i = 0;i<3;i++){
        printf(" Su Mo Tu We Th Fr Sa ");
    }
    printf("\n");
    //January, February and March
    //first line of days...
    int record1 = 0;
    int record2 = 0;
    int record3 = 0;
        for(int k = 0;k<findStartDateInMonth(1,year);k++){
            printf("   ");
        }
        for(int j = 1;j<=daysInMonth[1];j++){
            printf("%3d",j);
            if((j+findStartDateInMonth(1,year)) % 7 == 0){
                break;
                record1 = j + findStartDateInMonth(1,year) +1;
                printf("%d",record1);
            }
        }
        printf(" ");
        for(int k = 0;k<findStartDateInMonth(2,year);k++){
            printf("   ");
        }
        for(int j = 1;j<=daysInMonth[2];j++){
            printf("%3d",j);
            if((j+findStartDateInMonth(2,year)) % 7 == 0){
                break;
                record2 = j + findStartDateInMonth(2,year) +1;
                printf("%d",record2);
            }
        }
        printf(" ");
        for(int k = 0;k<findStartDateInMonth(3,year);k++){
            printf("   ");
        }
        for(int j = 1;j<=daysInMonth[3];j++){
            printf("%3d",j);
            if((j+findStartDateInMonth(3,year)) % 7 == 0){
                break;
                record3 = j + findStartDateInMonth(3,year) +1;
                printf("%d",record3);
            }
        }
        printf("\n");
        //second line
        /*
        for(int i = record1;i<record1+7;i++){
            printf("%3d",i);
        }
        printf(" ");
        for(int i = record2;i<record2+7;i++){
            printf("%3d",i);
        }
        for(int i = record2;i<record2+7;i++){
            printf("%3d",i);
        }
        */
        printf("\n");

It prints out the first line for the days, however, when I try to save the value of the day that will be in the newline for future printf statements, the values are not printed out as shown in the

printf("%d",record1);

Here is the current output of my program:

                               2017
    January               February               March
 Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa  Su Mo Tu We Th Fr Sa 
  1  2  3  4  5  6  7            1  2  3  4            1  2  3  4

What is my error in assigning values to record1,record2 and record3 and how would I fix it.

I'm sorry for the long post and I would really appreciate any help

Upvotes: 2

Views: 74

Answers (2)

chqrlie
chqrlie

Reputation: 144951

The break statements are misplaced in the day printing loops. You should break out of the loops after updating the running day number:

        if ((j + findStartDateInMonth(1, year)) % 7 == 0) {
            record1 = j + findStartDateInMonth(1, year) + 1;
            break;
        }

Your approach can be simplified and made more generic. For illustrative purposes, here is a more complete version:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

static const char *MonthNames[] = {
    NULL,
    "January", "February", "March", "April", "May", "June",
    "July", "August", "September", "October", "November", "December",
};

/* Print the Proleptic* Gregorian calendar
   (*) extended into the past before its adoption */
static void printYear(int year) {
    int DaysInMonth[] = { 0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
    const char *DayHeadings = "Su Mo Tu We Th Fr Sa";
    int d1, d2, d3, y1, day;

    y1 = year - 1;
    day = y1 * 1461 / 4 - y1 / 100 + y1 / 400 + 1;
    if (!(year % 4) && ((year % 100) || !(year % 400)))
        DaysInMonth[2] = 29;

    printf("%33d\n\n", year);
    for (int m = 1; m <= 12; m += 3) {
        int pad = 0;
        for (int i = 0; i < 3; i++) {
            int len = strlen(MonthNames[m + i]);
            int pad1 = len + (20 - len) / 2;
            printf("%*s", pad + pad1, MonthNames[m + i]);
            pad = 22 - pad1;
        }
        printf("\n%s  %s  %s\n", DayHeadings, DayHeadings, DayHeadings);
        d1 = 1 - day % 7;
        day += DaysInMonth[m];
        d2 = 1 - day % 7;
        day += DaysInMonth[m + 1];
        d3 = 1 - day % 7;
        day += DaysInMonth[m + 2];
        for (int j = 0; j < 6; j++) {
            pad = -1;
            for (int i = 0; i < 7; i++, d1++) {
                pad += 3;
                if (d1 > 0 && d1 <= DaysInMonth[m])
                    pad -= printf("%*d", pad, d1);
            }
            pad += 1;
            for (int i = 0; i < 7; i++, d2++) {
                pad += 3;
                if (d2 > 0 && d2 <= DaysInMonth[m + 1])
                    pad -= printf("%*d", pad, d2);
            }
            pad += 1;
            for (int i = 0; i < 7; i++, d3++) {
                pad += 3;
                if (d3 > 0 && d3 <= DaysInMonth[m + 2])
                    pad -= printf("%*d", pad, d3);
            }
            printf("\n");
        }
    }
}

int main(int argc, char *argv[]) {
    if (argc > 1) {
        for (int i = 1; i < argc; i++) {
            int year = strtol(argv[i], NULL, 0);
            printYear(year);
        }
    } else {
        printYear(2018);
    }
    return 0;
}

Upvotes: 1

Addison
Addison

Reputation: 8377

First of all, this looks like a really cool program.

Your code is quite good so far - You just have to move the break; statement to after the other commands, or it'll end the loop before they execute. Remember to do this for all 3 months.

for(int j = 1;j<=daysInMonth[1];j++){
    printf("%3d",j);
    if((j+findStartDateInMonth(1,year)) % 7 == 0){
        record1 = j + findStartDateInMonth(1,year) +1;
        break; // Moved this line to AFTER the record update
    }
}

And one more issue you might run into is:

record1 = j + findStartDateInMonth(1,year) +1;

This line should just be:

record1 = j + 1;

Try it online! Just hit "Run", and scroll to see the output.

Upvotes: 0

Related Questions