calander

This is a discussion on calander within the C Programming forums, part of the General Programming Boards category; i am trying to make a program for a calander which has the user input the amount of days in ...

  1. #1
    Unregistered
    Guest

    Question calander

    i am trying to make a program for a calander which has the user input the amount of days in each month and and the start day eg monday=0 and then display a calander for the month displaying it in lines with the correct amount of days starting with the specified start day. I am very new to programming so could nayone give me a few hints on how to achieve this? Would be greatly appreciated!!!

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Write pseudo code:
    Code:
        ask user for days in months (assuming all are equal?)
        ask user for a day to start on
        for each month
            starting on day (user inputted day) print the day number
            if the day number == whatever ends the week then make a new line
            if the day == the month-end-number then end the month
    You'll find that breaking things into words you can understand helps a lot.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    how would i go about arranging it in table form tho so it starts on the correct day and has right amount of days in month

  4. #4
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Read,learn from..... ask if you dont understand anything.....
    Code:
    #include<windows.h>
    #include<stdio.h>
    #include<stdlib.h>
    #include<math.h>
    #include<string.h>
    
    struct
    {
    	int day;
    	int month;
    	int year;
    }calender;
    
    void clrscr() 
    { 
    COORD coordScreen = { 0, 0 }; 
    DWORD cCharsWritten; 
    CONSOLE_SCREEN_BUFFER_INFO csbi; 
    DWORD dwConSize; 
    HANDLE hConsole = GetStdHandle(STD_OUTPUT_HANDLE); 
    
    GetConsoleScreenBufferInfo(hConsole, &csbi); 
    dwConSize = csbi.dwSize.X * csbi.dwSize.Y; 
    FillConsoleOutputCharacter(hConsole, TEXT(' '), dwConSize, coordScreen, &cCharsWritten); 
    GetConsoleScreenBufferInfo(hConsole, &csbi); 
    FillConsoleOutputAttribute(hConsole, csbi.wAttributes, dwConSize, coordScreen, &cCharsWritten); 
    SetConsoleCursorPosition(hConsole, coordScreen); 
    } 
    
    void gotoxy(int x, int y) 
    { 
    COORD point; 
    point.X = x; point.Y = y; 
    SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE),point); 
    } 
    
    
    ////////////////////////////////////////////////////////////////////////////////////////////// 
    // 
    // isleapyear() 
    // 
    // checks whether year is a leap year in the gregorian calender 
    // a year is leap if it is divisable by four but not if divisible by 100 
    // unless it is divisible by 400 
    // 
    // returns true if leap and false if not 
    // 
    ////////////////////////////////////////////////////////////////////////////////////////////// 
    
    bool isleapyear() 
    { 
    if ((calender.year%400==0) || ((calender.year %4==0) && (calender.year%100 !=0))) 
    
    { 
    return true; // its a leap year 
    } 
    
    else 
    
    { 
    return false; // its not a leap year 
    } 
    } 
    
    ////////////////////////////////////////////////////////////////////////////////////////////// 
    // 
    // int howmanydays() 
    // 
    // returns number of days in month 
    // 
    ////////////////////////////////////////////////////////////////////////////////////////////// 
    
    int howmanydays() 
    { 
    static const int daysinmonth[12]={31,28,31,30,31,30,31,31,30,31,30,
    31}; 
    if((calender.month==2)&& isleapyear()) 
    { 
    return 29; // feb has 29 days in a leap year 
    } 
    return daysinmonth[calender.month-1]; 
    }
    
    ////////////////////////////////////////////////////////////////////////////////////////////// 
    // 
    // checkday(int) 
    // 
    // checks day is legal for month 
    // 
    // in:- day to be tested 
    // 
    // returns day if legal and 1 if not legal 
    // 
    ////////////////////////////////////////////////////////////////////////////////////////////// 
    
    int checkday(int testday) 
    { 
    
    
    if ((testday>0) && (testday<= howmanydays())) 
    
    { 
    return testday; // day is valid for month 
    } 
    printf("\n%s%d%s%d%s%d%s\n","Invalid day entered ",testday,"/",calender.month,"/",calender.year,". Setting to the first of the month.");
    
    return 1; // hopefully wont get here but if invalid day entered day is set to 1 
    } 
     
    
    ////////////////////////////////////////////////////////////////////////////////////////////// 
    // 
    // int whatdayisfirstofmonth() 
    // 
    // uses zeller's algorithm to find out what day the first of the month falls on falls on. 
    // 
    // returns 0 to 6 corresponding to sunday to saturday. 
    // 
    ////////////////////////////////////////////////////////////////////////////////////////////// 
    
    int whatdayisfirstofmonth() 
    { 
    int f,c,d,m,k;
    double g;
    c=calender.year/100; // # of centuries 
    d=calender.year%100; // # of years through century 
    m=(calender.month+10)%12; // # of month march is 1,feb is 12 
    k=1; // set the day part to 1 so we get back the day for first of month 
    if ((calender.month==1)||(calender.month==2))// treat jan and feb as if they were in previous year 
    { 
    if (d==0) // if d is 0 then to go back a year d becomes 99 and c become c-1 
    { 
    d=99; 
    c-=1; 
    } 
    else 
    { 
    d-=1; // jan and feb are treated as previous year 
    } 
    } 
    g=(k + (floor(((13*m)-1)/5)) + d + (floor(d/4)) + (floor(c/4)) - (2*c)); 
    f=(int)g%7; // cast result of algorithm to int to take modulus 
    if (f<0) // if negative add 7 
    
    { 
    f+=7; 
    } 
    return f; // returns 0 to 6 corresponding to sunday to saturday 
    } 
    
    void initcalender(int d,int m,int y) 
    { 
    
    if (y<1582) // church accepted gregorian calender in 1582 
    
    { 
    printf("\n%s%d%s\n","The year ",y," is before the gregorian calender was accepted by the church.Setting to 2001.");
    
    calender.year=2001; // invalid year so set to 2001 
    } 
    
    else 
    
    { 
    calender.year=y; // y is valid so use it to set year 
    } 
    
    if ((m>=1) && (m<=12)) // check month between 1 and 12 
    
    { 
    calender.month=m; // if it is set month 
    } 
    
    else 
    
    {
    printf("\n%s%d%s\n","The month ",m," is invalid.Setting to month 1");
    calender.month=1; 
    } 
    
    calender.day=checkday(d); // validate the day 
    } 
    
    char* newgets(char* buffer,int num)
    {
    int i;
    fgets(buffer,num,stdin);
    i=strlen(buffer)-1;
    if (buffer[i]=='\n') buffer[i]='\0';
    return buffer;
    }
    
    void printcalender()
    { 
    int i,j,startday,endday,rows,count;	
    clrscr();
    printf("%s%d%s%d%s%d\n","Date entered was :- ",calender.day,"/",calender.month,"/",calender.year); 
    gotoxy(4,2);
    printf("SUN");
    gotoxy(12,2);
    printf("MON");
    gotoxy(21,2);
    printf("TUES");
    gotoxy(31,2);
    printf("WED");
    gotoxy(42,2);
    printf("THUR");
    gotoxy (50,2);
    printf("FRI");
    gotoxy(60,2);
    printf("SAT"); 
    startday=whatdayisfirstofmonth(); 
    endday=howmanydays(); 
    for (i=0;i<startday;i++) 
    { 
    if (i==0) 
    
    { 
    gotoxy(4,4); 
    printf("-"); 
    } 
    if (i==1) 
    { 
    gotoxy(12,4); 
    printf("-"); 
    } 
    if (i==2) 
    { 
    gotoxy(21,4); 
    printf("-"); 
    } 
    if (i==3) 
    { 
    gotoxy(31,4); 
    printf("-");
    } 
    if (i==4) 
    { 
    gotoxy(42,4); 
    printf("-"); 
    } 
    if (i==5) 
    { 
    gotoxy(50,4); 
    printf("-"); 
    } 
    
    } // end of for loop 
    
    
    rows=4; 
    count=1; 
    for(j=startday;j<(startday+endday);j++) 
    { 
    if(j%7==0) 
    { 
    rows+=2; 
    gotoxy(4,rows); 
    } 
    if(j%7==1) gotoxy(12,rows); 
    if(j%7==2) gotoxy(21,rows); 
    if(j%7==3) gotoxy(31,rows); 
    if(j%7==4) gotoxy(42,rows); 
    if(j%7==5) gotoxy(50,rows); 
    if(j%7==6) gotoxy(60,rows); 
    if(count==calender.day) // set text to bright red if count is the day entered 
    { 
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED);
     
    } 
    else 
    { 
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN);
    
     
    } 
    
    printf("%d",count); 
    count ++; 
    } // end of for loop 
    printf("\n\n\n"); 
    } 
    
    
    
    
    int main() 
    { 
    int d,m,y;
    char input[20];
    char* p;
    while (1) 
    { 
    
    clrscr(); 
    SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),FOREGROUND_INTENSITY|FOREGROUND_RED|FOREGROUND_BLUE|FOREGROUND_GREEN);
    printf("Welcome to the gregorian calender calculator. v.1.00\n\n");
    printf("Use UK format.... day/month/full year. Use the '/' char to delimit your entry...\n\n");
    printf("Please enter the date you would like to see the calender for :- "); 
    newgets(input,20);
    printf("\n");
    
    
    d=atoi(input);
    p=strchr(input,'/');
    m=atoi(++p); 
    p=strchr(p,'/');
    y=atoi(++p); 
    initcalender(d,m,y);
    system("PAUSE"); // wait for a keypress 
    printcalender(); // print the calender 
    printf("Another calender (Y/N) ? "); 
    newgets(input,20);
    if ((input[0]=='N') || (input[0]=='n')) 
    { 
    break; // drop out of while loop 
    } 
    } // end of while loop 
    
    
    return 0; 
    }
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  5. #5
    Unregistered
    Guest
    i have looked at this before from a previous post but this gives you the option to insert a date and a year before displaying the calander. What i am looking to do is actaully input the amount of days in the month and also the start day of the month then display the calander for the particular month. I am not interested in leap years or in fact different years just user inputted date to produce the month calander.
    any suggestions?
    thank you for your time!

  6. #6
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    Look at the printcalender function that is pretty much what it does.
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

  7. #7
    Registered User
    Join Date
    Jan 2002
    Posts
    381
    geee Stoned_Coder, you make it look soo easy. . . .

  8. #8
    B26354 Deckard's Avatar
    Join Date
    Jan 2002
    Posts
    633
    You may also find this thread useful.
    Jason Deckard

  9. #9
    Registered User
    Join Date
    Sep 2001
    Posts
    752
    Hmm...
    Code:
    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
    That's our goal. Looking at it, we can tell mainly that we're gonna need to utilize conversion characters to align everything, and a for loop to run from day 1 to the final day. Since the calendar starts with Sun, you should probably let sunday be the day for 0, and just let them count up to saturday, which will be day 6.

    Here's a snippet of the program...
    Code:
    #include <stdio.h>
    
    // If you don't know what enum is, then feel free to comment this
    //  out, it's unneccisary, but might be helpful in some situations.
    enum {Sun, Mon, Tue, Wed, Thu, Fri, Sat};
    
    void PrintEmptyDays (int firstDay);
    void PrintDay (int currDay);
    int isSaturday (int currDay, int firstDay);
    
    int main (void)
    {
     int firstDay, daysInMonth;
    
     // I'm assuming the user will input 0 for Sun, 1 for Mon, etc...
     printf ("First day of month please? ");
     scanf ("%d", &firstDay);
    
     printf ("Number of days in month please? ");
     scanf ("%d", &DaysInMonth);
    
     printf ("Sun Mon Tue Wed Thu Fri Sat\n");
    
     PrintEmptyDays (firstDay);
    
     // Now the cursor is (hopefully) at the position for the first day.
     // Time to start printing the days....
     for (i = 1; i <= daysInMonth; i++)
     {
      PrintDay (i);
      if (isSaturday(i, firstDay))
      {
       printf ("\n");
      }
     }
    
     return 0;
    }
    I suggest that you implment the functions one at a time, PrintDay first. You can just fill the other functions with nothing...
    Code:
    void PrintEmptyDays (int firstday)
    {
     return;
    }
    
    int isStaturday (int currDay, int firstDay)
    {
     return 0;
    }
    PrintDay is a very simple function, you need to use a conversion char in such a manner that the day will be properly aligned. PrintEmptyDays is just a for loop with a printf printing spaces. isSaturday returns 0 if the current day is not saturday, and returns non-zero otherwise, just some simple math.
    Callou collei we'll code the way
    Of prime numbers and pings!

  10. #10
    Unregistered
    Guest
    i tired to copile the above code and it told me days in mionth not declared but it is declared in the intt after main isnt it??

  11. #11
    Skunkmeister Stoned_Coder's Avatar
    Join Date
    Aug 2001
    Posts
    2,572
    scanf ("%d", &DaysInMonth);

    should be....

    scanf ("%d", &daysInMonth);
    Free the weed!! Class B to class C is not good enough!!
    And the FAQ is here :- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Mayan Calander Conversion Issue
    By AKalair in forum C Programming
    Replies: 1
    Last Post: 11-30-2007, 11:26 AM
  2. About the Calander control 10.0
    By frankiepoon in forum Windows Programming
    Replies: 0
    Last Post: 10-16-2002, 10:50 PM
  3. Need to write program with outcome of a calander
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 02-06-2002, 11:21 AM
  4. extending a month calander to a year
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 02-03-2002, 06:39 AM

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