-
ending lines
I need to output the following to a screen:
SUN MON TUE WED THU FRI SAT
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
this is my code so far:
#include<stdio.h>
#include<conio.h>
main()
{
int first_day,
days_in_month;
printf("Enter day on which the 1st falls 1 = Sunday 2 = Monday etc : ");
scanf("%d", &first_day);
printf("How many days the month? : ");
scanf("%d", &days_in_month);
printf("\n SUN MON TUE WED THU FRI SAT\n");
int columns = 1;
while (columns <= first_day -1)
{
printf(" ");
columns += 1;
}
int day_index = 1;
while (day_index <= days_in_month)
{
if (columns = 7)
{
printf("%4d", day_index);
columns = 1;
printf("\n");
}
else
{
printf("%d", day_index);
columns += 1;
}
day_index += 1;
}
getch();
}
It's not ending the line as I need it to. Any suggestions?
:confused:
-
Code:
#include<stdio.h>
#include<conio.h>
int main() {
int first_day, days_in_month;
int columns = 1;
int day_index = 1; // should be declared here - this is C, not C++
printf( "Enter day on which the 1st falls 1 = Sunday 2 = Monday etc : " );
scanf( "%d", &first_day );
printf( "How many days the month? : " );
scanf( "%d", &days_in_month );
printf( "\n SUN MON TUE WED THU FRI SAT\n" );
while(columns <= first_day -1) {
printf( " " );
columns += 1;
}
while(day_index <= days_in_month) {
if(columns == 7) // should be ==
{
printf( "%4d", day_index );
columns = 1;
printf( "\n" );
} else {
printf( "%4d", day_index ); // print this in a 4 width as well
columns += 1;
}
day_index += 1;
}
getch( );
}