Help With Aligning

This is a discussion on Help With Aligning within the C Programming forums, part of the General Programming Boards category; Hello! I am currently in the process of creating a program that will output a calendar given the number of ...

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    28

    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:

    Name:  calendar.png
Views: 1074
Size:  6.7 KB

    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

  2. #2
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,699
    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.

  3. #3
    Registered User
    Join Date
    Feb 2013
    Location
    Buea Cameroon
    Posts
    64

    Post 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);
        }
    
    
     }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Right aligning.
    By theCanuck in forum C++ Programming
    Replies: 2
    Last Post: 02-12-2011, 11:50 AM
  2. Aligning Columns
    By mike_g in forum C Programming
    Replies: 3
    Last Post: 06-27-2007, 08:27 AM
  3. Aligning text
    By Bad_Scooter in forum C++ Programming
    Replies: 5
    Last Post: 06-04-2003, 12:06 PM
  4. aligning text
    By Unregistered in forum C++ Programming
    Replies: 3
    Last Post: 04-01-2002, 08:43 AM
  5. Aligning in a TListBox
    By tegwin in forum C++ Programming
    Replies: 0
    Last Post: 03-09-2002, 04:32 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21