i having litte problem loop. have calendar. created procedure print calendar using data comes other functions (as if year leap year example). want function write numbers number of days of month, inserting space every week. programm not finished (right considers months starting monday) right prints 35 or 36 numbers. if 31 or 30 comes monday or wednesday loop doesn't finish keeps writting whole rest of week. expected end on 30 or 31 value month_days can have. problem not on variable, when print it gives me right value (30 or 31 depending on month) can failing on loop structure?
void imprimir_calendario (int mes, int anho, int month_days, int primer_dia_mes) { int index = 1; int printed_days= 1; printf ("\n%d\n",month_days); /*test sentence returns 30 or 31 should*/ printf ("===========================\n"); printf ("== month & year ==\n"); printf ("===========================\n"); printf (" l m x j v s d\n"); while (printed_days < month_days) { while (index<=7){ printf (" %2d ",printed_days); printed_days++; index++;} index = 1; printf ("\n");} printf ("\n%d",printed_days);} /*test sentence, returns 35 or 36*/
once entered, inner loop runs 7 times ragardless of current value of printed_days.
you need check condition , break inner loop:
while (index<=7) { printf (" %2d ",printed_days); printed_days++; index++; if (printed_days > month_days) break; }
Comments
Post a Comment