-
1 Attachment(s)
Help With Aligning
Hello!
I am currently in the process of creating a program that will output a calendar given the number of days in the month and the day the month starts on. A sample output should look something like:
Attachment 12497
I am having the most challenging time getting the calendar to align to the starting day of the month. If the user entered Su and 30 days, it should enter 1-7 for Sunday through Saturday and then start a new line, and then continue aligning until it gets to Saturday again and start a new line. Here is my code:
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int days,week;
char day,x1,x2;
int
main(void)
{
printf("This program displays the calendar for a month.\n");
printf("Enter the number of days in the month:\n");
scanf("%i", &days);
printf("Enter starting day of the week (Su,Mo,Tu,...,Sa):\n");
scanf(" %c", &day);
char x1 = toupper(day);
char x2 = tolower(day);
printf("Su Mo Tu We Th Fr Sa\n");
for (int i = 1; i <= days; i = i + 1){
printf("%i%3c", i,' ');
}
}
*Ignore the characters x1 and x2
-
Try making a calendar on some grid paper first. Each grid cell is a character on the screen. Then you will see what to print on each line and where to break lines. It will help you realize all of the negative space you are ignoring at least.
-
this will do the trick
Code:
#include <stdio.h>
#include <ctype.h>
int k,i,days;
char x1, x2;
int main(void)
{
printf("This program displays the calendar for a month.\n");
printf("Enter the number of days in the month:");
scanf("%d",&days);
printf("Enter starting day of the week (Su,Mo,Tu,...,Sa): ");
scanf(" %c %c",&x1,&x2);
x1 = toupper(x1);
x2 = tolower(x2);
printf("Su\tMo\tTu\tWe\tTh\tFr\tSa\n");
//The aligning trick starts here
if(x1=='M'&&x2=='o'){k=1;}
if(x1=='T'&&x2=='u'){k=2;}
if(x1=='W'&&x2=='e'){k=3;}
if(x1=='T'&&x2=='h'){k=4;}
if(x1=='F'&&x2=='r'){k=5;}
if(x1=='S'&&x2=='a'){k=6;}
if(x1=='S'&&x2=='u'){k=0;}
//This loop does the aligning trick
for(i=0;i<k;i++){printf("\t");}
//This asignment is neccesary for the Calender to be printed
k-=7;
//This loop prints the Calender
for(i=1; i<=days ;k++, i++)
{
if(k%7==0&&i>1)
{ //This condition ensures that a new line is printed at end of each week
printf("\n");
}
printf("%2d\t",i);
}
}