Chat LIVE With Programming Experts! There Are 23 Online Right Now...

Welcome to Dream.In.Code
Become a C++ Expert!

Join 244,310 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 867 people online right now. Registration is fast and FREE... Join Now!




Need to shorten my code for a simple calendar-printing program.

 
Reply to this topicStart new topic

Need to shorten my code for a simple calendar-printing program., Beginner programmer needs help making the code efficient.

ShotokanDeity
13 Sep, 2007 - 10:26 PM
Post #1

D.I.C Head
Group Icon

Joined: 13 Sep, 2007
Posts: 79


My Contributions
Hello everyone. I have a program that prints a calendar out for the user. It prompts for the input (0-6 for the day the year starts on; 0 for non-leapyear and 1 for leapyear) and then prints out the year accordingly. However, I feel that my code is very long and inefficient and would like to shorten it. It would also like to be able to insert pauses where any key can be pressed once to continue the program. (I have seen the sticky post about this, but I do not understand the code for the ANSI standard ways.)

This is my first-ever C/C++ program and apologize for the incompetence. Any help is greatly appreciated!

Here is my code (I apologize for the monstrous length -- almost 400 lines!):
CODE

/* -------------------------------- */
/* This is a Calendar program       */
/* Name: Kevin VanUs                */
/* Course: COP 3514-001             */
/* Date: September 21 2007          */
/* -------------------------------- */

/* -------------------------------- */
/* Input Parameters: 0 Sunday       */
/*                     1 Monday        */
/*                     2 Tuesday        */
/*                     3 Wednesday    */
/*                     4 Thursday        */
/*                     5 Friday        */
/*                     6 Saturday        */
/*                                  */
/*                   0 Non-Leapyear */
/*                   1 Leapyear     */
/* -------------------------------- */

#include <stdio.h>

/* Start body of program */
int main(void)
{
    int leapyear;        /* specifies whether or not the year is a leapyear */
    int daycode;        /* specifies the first day of the year/month */
    int daysinmonth;    /* says how many days are in the current month */
    int day;        /* the day number which increments until daysinmonth is reached */
    int month = 1;        /* variable used to increment the main 'while' loop */
    int skip = 0;        /* the variable to say how many days to skip */

    /* This segment prompts for and analyzes the user input */
    printf("Enter the day and leapyear codes: ");
    scanf ("%i%i", &daycode, &leapyear);
    while (daycode < 0 || daycode > 6)    /* these two 'while' statements are the error controls */
    {
        printf ("Please enter a proper day and leapyear code: ");
        scanf ("%i%i" , &daycode, &leapyear);
    }
    while (leapyear < 0 || leapyear > 1)
    {
        printf ("Please enter a proper day and leapyear code: ");
        scanf ("%i%i", &daycode, &leapyear);
    }
    /* This segment is just the formulas the program uses */
    /* skip = 1 + daycode*5;                    --- the first day is printed after skipping this amount */
    /* (day + daycode) % 7;                        --- to check to if we are at the end of the week */
    /* daycode = (daycode + daysinmonth) % 7;    --- code to see if we go to the next month */
    /* skip = daycode - 1;                        --- number of days to print blank spaces */

    /* This segment processes the input and prints out the calendar for the year */
    while (month <= 12)
    {
        while (month == 1)
        {
            skip = 0;
            day = 1;
            daysinmonth = 31;
            puts ("\n\n\nJanuary\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 2)
        {
            skip = 0;
            day = 1;
            if (leapyear == 1)
            {
                daysinmonth = 29;
            }
            else
            {
                daysinmonth = 28;
            }

            puts ("\n\n\nFebruary\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 3)
        {
            skip = 0;
            day = 1;
            daysinmonth = 31;
            puts ("\n\n\nMarch\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 4)
        {
            skip = 0;
            day = 1;
            daysinmonth = 30;
            puts ("\n\n\nApril\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 5)
        {
            skip = 0;
            day = 1;
            daysinmonth = 31;
            puts ("\n\n\nMay\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 6)
        {
            skip = 0;
            day = 1;
            daysinmonth = 30;
            puts ("\n\n\nJune\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 7)
        {
            skip = 0;
            day = 1;
            daysinmonth = 31;
            puts ("\n\n\nJuly\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 8)
        {
            skip = 0;
            day = 1;
            daysinmonth = 31;
            puts ("\n\n\nAugust\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 9)
        {
            skip = 0;
            day = 1;
            daysinmonth = 30;
            puts ("\n\n\nSeptember\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 10)
        {
            skip = 0;
            day = 1;
            daysinmonth = 31;
            puts ("\n\n\nOctober\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 11)
        {
            skip = 0;
            day = 1;
            daysinmonth = 30;
            puts ("\n\n\nNovember\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 12)
        {
            skip = 0;
            day = 1;
            daysinmonth = 31;
            puts ("\n\n\nDecemeber\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
    }
    printf ("\n\n\nEnter any key to end program.");
    scanf ("%i");

    return 0;
}


This post has been edited by ShotokanDeity: 13 Sep, 2007 - 10:34 PM

User is offlineProfile CardPM
+Quote Post


Trogdor
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
14 Sep, 2007 - 01:03 AM
Post #2

D.I.C Addict
Group Icon

Joined: 6 Oct, 2006
Posts: 609



Thanked: 11 times
Dream Kudos: 200
My Contributions
You have written a block of code for each month.
Those blocks are nearly identical.
See if you can combine them into one single block of code, with in some places only the differences coded out. For example:
CODE

  if((month==1) || (month==3) || (month==5) ||  ... etc ){
    daysinmonth = 31;
  }else if(month == 2){
    daysinmonth = 28; // do the leap year thing here
  }else{
    daysinmonth = 30;
  }

Or even better: use an array to store that information and do an array lookup with the month-number as the index.

In this way you put the decisions in one place, and put all the shared code in one block instead of 12.
That should bring your code back to something more manegable.

This post has been edited by Trogdor: 14 Sep, 2007 - 01:03 AM
User is offlineProfile CardPM
+Quote Post

ShotokanDeity
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
14 Sep, 2007 - 04:00 PM
Post #3

D.I.C Head
Group Icon

Joined: 13 Sep, 2007
Posts: 79


My Contributions
Thanks for the help Trogdor! I would love to use an array (I used them with Java) but I haven't yet looked up arrays for C. I need to tinker around a bit more to figure out exactly how to implement your idea in my code, and thanks for replying! I look forward to having a much shorter code biggrin.gif
User is offlineProfile CardPM
+Quote Post

ShotokanDeity
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
17 Sep, 2007 - 11:39 AM
Post #4

D.I.C Head
Group Icon

Joined: 13 Sep, 2007
Posts: 79


My Contributions
OK, so its not posting my original post. This is not a bump! I am trying to post my revised code. I hope this edit finally works!

Yay it let me add my code this time!

OK, so I tried to implement the "if/else" idea suggested, and my problem now is that it won't output correctly. It compiles fine, but after it outputs the month of January correctly, all it does it output the month name and week days. Until it gets to October, which it then outputs correctly again, and then November and December are printed without day numbers. I cant figure out why.

I realize that an array would be much simpler, but I don't really understand them right now. Oh, and even though this is for a class, my original (and working!) code I had posted was perfectly OK and acceptable, but I would like to shorten it for my own personal satisfaction biggrin.gif

CODE


#include <stdio.h>
#include <iostream>        /* Included to allow for the "Press enter to continue" parts */
#include <limits>        /* Included to allow for the "Press enter to continue" parts */

/* Start body of program */
int main(void)
{
    int leapyear = 0;        /* specifies whether or not the year is a leapyear */
    int daycode = 0;        /* specifies the first day of the year/month */
    int daysinmonth = 0;    /* says how many days are in the current month */
    int day = 1;            /* the day number which increments until daysinmonth is reached */
    int month = 1;            /* variable used to increment the main 'while' loop */
    int skip = 0;            /* the variable to say how many days to skip */

    /* This segment prompts for and analyzes the user input */
    printf("Enter the day and leapyear codes: ");
    scanf ("%i%i", &daycode, &leapyear);
    while ((daycode < 0 || daycode > 6) || (leapyear < 0 || leapyear > 1))    /* this 'while' statement is the error control */
    {
        printf ("Please enter a proper day and leapyear code: ");
        scanf ("%i%i" , &daycode, &leapyear);
    }
    
    /* This segment is just the formulas the program uses */
    /* skip = 1 + daycode*5;                    --- the first day is printed after skipping this amount */
    /* (day + daycode) % 7;                        --- to check to if we are at the end of the week */
    /* daycode = (daycode + daysinmonth) % 7;    --- code to see if we go to the next month */
    /* skip = daycode - 1;                        --- number of days to print blank spaces */

    /* This segment processes the input and prints out the calendar for the year */
    while (month <= 12)
    {
            /*  This segment is a centralized decision-making area for all 12 months  */
        skip = 0;
        day = 1;
        if((month==1) || (month==3) || (month==5) || (month==7) || (month==8) || (month==10) || (month==12))
        {
            daysinmonth = 31;
        }
        else
        {
            if(month == 2)
            {
                if (leapyear == 1)
                {
                    daysinmonth = 29;
                }
                else
                {
                    daysinmonth = 28;
                }
            }
            else
            {
                daysinmonth = 30;
            }
        }
            /*  The rest of the code is the actual output code  */
        while (month == 1)
        {
            puts ("\n\n\nJanuary\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 2)
        {
            puts ("\n\n\nFebruary\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
            printf("\n\nPress enter to continue");
            std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );    /* this snippet of code is courtesy of */
            std::cin.get();                                /*        dreamincode.net.       */
        }
        while (month == 3)
        {
            puts ("\n\n\nMarch\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 4)
        {
            puts ("\n\n\nApril\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
            printf("\n\nPress enter twice to continue");
            std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
            std::cin.get();
        }
        while (month == 5)
        {
            puts ("\n\n\nMay\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 6)
        {
            puts ("\n\n\nJune\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
            printf("\n\nPress enter twice to continue");
            std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
            std::cin.get();
        }
        while (month == 7)
        {
            puts ("\n\n\nJuly\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 8)
        {
            puts ("\n\n\nAugust\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
            printf("\n\nPress enter twice to continue");
            std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
            std::cin.get();
        }
        while (month == 9)
        {
            puts ("\n\n\nSeptember\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 10)
        {
            skip = 0;
            day = 1;
            daysinmonth = 31;
            puts ("\n\n\nOctober\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
            printf("\n\nPress enter twice to continue");
            std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
            std::cin.get();
        }
        while (month == 11)
        {
            puts ("\n\n\nNovember\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
        while (month == 12)
        {
            puts ("\n\n\nDecemeber\n");
            puts ("Sun  Mon  Tue  Wed  Thu  Fri  Sat");
            while (day <= daysinmonth)
            {
                while ((day <= daysinmonth))
                {
                    while (skip < daycode)
                    {
                        printf ("     ");
                        skip = skip + 1;
                    }
                    printf ("%2i   ", day);
                    day = day + 1;
                    if (((day + daycode) % 7) == 1)
                    {
                        printf("\n");
                    }
                }
            }
            daycode = (daycode + daysinmonth) % 7;
            month = month + 1;
        }
    }
    printf("\n\nPress enter twice to end the program.");
    std::cin.ignore ( std::numeric_limits<std::streamsize>::max(), '\n' );
    std::cin.get();

    return 0;
}


This post has been edited by ShotokanDeity: 17 Sep, 2007 - 11:49 AM
User is offlineProfile CardPM
+Quote Post

Bench
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
17 Sep, 2007 - 12:09 PM
Post #5

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 827



Thanked: 57 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
Is this a C or a C++ program? What you have here is an uneasy mix of two similar, but fundamentally different languages.

if its C, then I'm afraid that you can't use <iostream> or <limits>, since these are C++ libraries. If you're writing C++, then you really ought to avoid the <stdio.h> library, and functions such as printf, scanf, etc.
User is offlineProfile CardPM
+Quote Post

ShotokanDeity
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
17 Sep, 2007 - 12:21 PM
Post #6

D.I.C Head
Group Icon

Joined: 13 Sep, 2007
Posts: 79


My Contributions
Well technically it is a C program. However, to put pauses in my program that comply with ANSI standards, I used the code snippet found here..

So yes it is an uneasy mix of the two, but solely for that purpose, as there is no other C++ within the program.
User is offlineProfile CardPM
+Quote Post

Bench
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
18 Sep, 2007 - 05:47 AM
Post #7

D.I.C Addict
Group Icon

Joined: 20 Aug, 2007
Posts: 827



Thanked: 57 times
Dream Kudos: 150
Expert In: C/C++

My Contributions
QUOTE(ShotokanDeity @ 17 Sep, 2007 - 09:21 PM) *

Well technically it is a C program. However, to put pauses in my program that comply with ANSI standards, I used the code snippet found here..

So yes it is an uneasy mix of the two, but solely for that purpose, as there is no other C++ within the program.

Unfortunately, C++ isn't ANSI C compliant, If you want to be ANSI C compliant, try something like this instead

CODE
    char ch;
    do
    {
        ch = getchar();
    } while( ch != EOF && ch != '\n' );
  
    printf("Press enter to continue...\n");
    getchar();


For convenience, you could wrap it all up into a neat function, then, every time you wish to pause the program, just call that function.
User is offlineProfile CardPM
+Quote Post

ShotokanDeity
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
18 Sep, 2007 - 06:50 AM
Post #8

D.I.C Head
Group Icon

Joined: 13 Sep, 2007
Posts: 79


My Contributions
QUOTE
Unfortunately, C++ isn't ANSI C compliant

...heh good point!

I like the idea of putting the pause in a function, but I'm not sure how to do that. Anyway, thanks for the post and I will tinker around some more to see what I get.

Oh, so with the function thing, is that what I'm supposed to do with this part as well?

CODE

            /*  This segment is a centralized decision-making area for all 12 months  */
        skip = 0;
        day = 1;
        if((month==1) || (month==3) || (month==5) || (month==7) || (month==8) || (month==10) || (month==12))
        {
            daysinmonth = 31;
        }
        else
        {
            if(month == 2)
            {
                if (leapyear == 1)
                {
                    daysinmonth = 29;
                }
                else
                {
                    daysinmonth = 28;
                }
            }
            else
            {
                daysinmonth = 30;
            }
        }


Alrighty, thanks again for the help anime1.gif
User is offlineProfile CardPM
+Quote Post

mario_dfm
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
26 Sep, 2007 - 08:20 AM
Post #9

New D.I.C Head
*

Joined: 23 Sep, 2007
Posts: 8


My Contributions
I got some of the ideas from the program that you made...I'm still a freshman so i'm only using basic stuff...the program I made preety much works and fits 2 pages of bond papers...

CODE

#include<stdio.h>
#include<conio.h>

int Calendar(int daycode,int year,int leapyear);

void main(void)
{
int year;
int daycode;
int leapyear;

clrscr();

printf("Enter a year: ");
scanf("%d",&year);
leapyear=(!(year%4)&&(year%100))||!(year%4);
daycode=((year-1)*365+((year-1)/4)-((year-1)/100)+((year-1)/400)+2)%7;
Calendar(daycode,year,leapyear);
getch();
}

int Calendar(int daycode,int year,int leapyear)
{
int month;
int skip;
int day;
int daysinmonth;


for(month=1;month<=12;month++)
{
skip=0;
day=1;
if (month==1||month==3||month==5||month==7||month==8||month==10||month==12)
   daysinmonth=31;
else if(month==4||month==6||month==9||month==11)
     daysinmonth=30;
     else if(month==2)
      if(leapyear==1)
      daysinmonth=29;
      else if(leapyear==0)
           daysinmonth=28;
switch(month)
{
case 1: printf("\n\nJANUARY                     %d\n",year);break;
case 2: printf("\n\nFEBRUARY                    %d\n",year);break;
case 3: printf("\n\nMARCH                       %d\n",year);break;
case 4: printf("\n\nAPRIL                       %d\n",year);break;
case 5: printf("\n\nMAY                         %d\n",year);break;
case 6: printf("\n\nJUNE                        %d\n",year);break;
case 7: printf("\n\nJULY                        %d\n",year);break;
case 8: printf("\n\nAUGUST                      %d\n",year);break;
case 9: printf("\n\nSEPTEMBER                   %d\n",year);break;
case 10: printf("\n\nOCTOBER                     %d\n",year);break;
case 11: printf("\n\nNOVEMBER                    %d\n",year);break;
case 12: printf("\n\nDECEMBER                    %d\n",year);
}
printf ("SUN  MON  TUE  WED  THU  FRI SAT\n");

while(day<=daysinmonth)
{
  while(day<=daysinmonth)
  {
      while(skip<daycode)
      {
    printf("     ");
    skip=skip+1;
      }
      printf("%2d   ",day);
      day=day+1;
      if(((day+daycode)%7)==1)
      {
    printf("\n");
      }
  }
}
daycode=(daycode+daysinmonth)%7;
getch();
}
getch();
}

User is offlineProfile CardPM
+Quote Post

Codegamer
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
15 May, 2008 - 01:19 AM
Post #10

D.I.C Head
**

Joined: 4 May, 2008
Posts: 127


My Contributions
That's a long code lol!


Use this:

CODE

#include<iostream.h>
#include<conio.h>


int showmonth(int m,int y,int n);
int main()
{         char x;
          x='y';
          while(x=='y')
          {
          int n,y;
          cout<<"Enter any year: ";
          cin>>y;
          cout<<"\n\n\n\t\tCALENDAR OF THE YEAR  ";
          cout<<y<<endl;
          n=(y-1)*(365+0.25-0.01+0.0025);
          n++;
          for(int k=1;k<=12;k++)
          {
          n=showmonth(k,y,n);
          }
          cout<<"If you want to continue press 'y'\n Enter here:  ";
          cin>>x;
          }
          getch();
          return 0;
}
int showmonth(int m,int y,int n)
{
       int d=1,s=1,k=31,p;
       switch(m)
       {
case 1 :cout<<"\nJANUARY\n";
        k=31;
        break;
case 2 :cout<<"\nFEBRUARY\n";
        if(((y%4==0)&&(y%100!=0))||(y%400==0))
        k=29;
        else
        k=28;
        break;
case 3: cout<<"\nMARCH\n";
        k=31;
        break;
case 4: cout<<"\nAPRIL\n";
        k=30;
        break;
case 5:cout<<"\nMAY\n";
        k=31;
        break;
case 6:cout<<"\nJUNE\n";
        k=30;
        break;
case 7:cout<<"\nJULY\n";
        k=31;
        break;
case 8:cout<<"\nAUGUST\n";
        k=31;
        break;
case 9:cout<<"\nSEPTEMBER\n";
        k=30;
        break;
case 10:cout<<"\nOCTOBER\n";
        k=31;
        break;
case 11:cout<<"\nNOVEMBER\n";
        k=30;
        break;
case 12:cout<<"\nDECEMBER\n";
        k=31;
        break;
}
cout<<endl<<y<<endl;
n=n%7;
cout<<"\n-----------------------------------------------------\n";
cout<<"SUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\t\n";
cout<<"\n-----------------------------------------------------\n";
while(s<=n)
{
        cout<<"\t";
        s++;
}
while((n<7)&&(d<=k))
{
while((n<7)&&(d<=k))
{
cout<<d<<"\t";
n++;
d++;
}
cout<<"\n";
p=n;
n=0;
}
cout<<"\n-----------------------------------------------------\n";
return(p);
}




User is offlineProfile CardPM
+Quote Post

piotrekpepe
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
12 May, 2009 - 01:50 PM
Post #11

New D.I.C Head
*

Joined: 12 May, 2009
Posts: 4

QUOTE(Codegamer @ 15 May, 2008 - 01:19 AM) *

That's a long code lol!


Use this:

CODE

#include<iostream.h>
#include<conio.h>


int showmonth(int m,int y,int n);
int main()
{         char x;
          x='y';
          while(x=='y')
          {
          int n,y;
          cout<<"Enter any year: ";
          cin>>y;
          cout<<"\n\n\n\t\tCALENDAR OF THE YEAR  ";
          cout<<y<<endl;
          n=(y-1)*(365+0.25-0.01+0.0025);
          n++;
          for(int k=1;k<=12;k++)
          {
          n=showmonth(k,y,n);
          }
          cout<<"If you want to continue press 'y'\n Enter here:  ";
          cin>>x;
          }
          getch();
          return 0;
}
int showmonth(int m,int y,int n)
{
       int d=1,s=1,k=31,p;
       switch(m)
       {
case 1 :cout<<"\nJANUARY\n";
        k=31;
        break;
case 2 :cout<<"\nFEBRUARY\n";
        if(((y%4==0)&&(y%100!=0))||(y%400==0))
        k=29;
        else
        k=28;
        break;
case 3: cout<<"\nMARCH\n";
        k=31;
        break;
case 4: cout<<"\nAPRIL\n";
        k=30;
        break;
case 5:cout<<"\nMAY\n";
        k=31;
        break;
case 6:cout<<"\nJUNE\n";
        k=30;
        break;
case 7:cout<<"\nJULY\n";
        k=31;
        break;
case 8:cout<<"\nAUGUST\n";
        k=31;
        break;
case 9:cout<<"\nSEPTEMBER\n";
        k=30;
        break;
case 10:cout<<"\nOCTOBER\n";
        k=31;
        break;
case 11:cout<<"\nNOVEMBER\n";
        k=30;
        break;
case 12:cout<<"\nDECEMBER\n";
        k=31;
        break;
}
cout<<endl<<y<<endl;
n=n%7;
cout<<"\n-----------------------------------------------------\n";
cout<<"SUN\tMON\tTUE\tWED\tTHU\tFRI\tSAT\t\n";
cout<<"\n-----------------------------------------------------\n";
while(s<=n)
{
        cout<<"\t";
        s++;
}
while((n<7)&&(d<=k))
{
while((n<7)&&(d<=k))
{
cout<<d<<"\t";
n++;
d++;
}
cout<<"\n";
p=n;
n=0;
}
cout<<"\n-----------------------------------------------------\n";
return(p);
}


How to save the calendar to a file *. txt ?
For example. After a query to which I want to get the calendar year, the program displays it and than saves it in a *.txt file.

This post has been edited by piotrekpepe: 12 May, 2009 - 01:50 PM
User is offlineProfile CardPM
+Quote Post

janotte
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
13 May, 2009 - 03:00 AM
Post #12

code > sword
Group Icon

Joined: 28 Sep, 2006
Posts: 2,137



Thanked: 150 times
Expert In: C/C++

My Contributions
QUOTE(piotrekpepe @ 12 May, 2009 - 01:50 PM) *

How to save the calendar to a file *. txt ?
For example. After a query to which I want to get the calendar year, the program displays it and than saves it in a *.txt file.


Here you go
http://www.cprogramming.com/tutorial/lesson10.html



User is offlineProfile CardPM
+Quote Post

piotrekpepe
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
13 May, 2009 - 04:33 AM
Post #13

New D.I.C Head
*

Joined: 12 May, 2009
Posts: 4

But I do not know how to put all what I have (calendar, for example 2009) to *.txt
User is offlineProfile CardPM
+Quote Post

janotte
RE: Need To Shorten My Code For A Simple Calendar-printing Program.
13 May, 2009 - 05:06 AM
Post #14

code > sword
Group Icon

Joined: 28 Sep, 2006
Posts: 2,137



Thanked: 150 times
Expert In: C/C++

My Contributions
QUOTE(piotrekpepe @ 13 May, 2009 - 04:33 AM) *

But I do not know how to put all what I have (calendar, for example 2009) to *.txt


1 - Start your own thread asking this question instead of hijacking someone else's thread.

2 - When you start your own thread post the code you have written so far trying to do what you need to do.



User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic

Time is now: 7/4/09 07:28PM

Live C++ Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month