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

Beginner programmer needs help making the code efficient.

Page 1 of 1

13 Replies - 6889 Views - Last Post: 13 May 2009 - 06:06 AM Rate Topic: -----

#1 ShotokanDeity  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 13-September 07

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

Post icon  Posted 13 September 2007 - 11:26 PM

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!):
/* -------------------------------- */
/* 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 September 2007 - 11:34 PM


Is This A Good Question/Topic? 0
  • +

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

#2 Trogdor  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 15
  • View blog
  • Posts: 627
  • Joined: 06-October 06

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

Posted 14 September 2007 - 02:03 AM

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:
  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 September 2007 - 02:03 AM

Was This Post Helpful? 0
  • +
  • -

#3 ShotokanDeity  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 13-September 07

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

Posted 14 September 2007 - 05:00 PM

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 :D
Was This Post Helpful? 0
  • +
  • -

#4 ShotokanDeity  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 13-September 07

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

Posted 17 September 2007 - 12:39 PM

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 :D

#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 September 2007 - 12:49 PM

Was This Post Helpful? 0
  • +
  • -

#5 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

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

Posted 17 September 2007 - 01:09 PM

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.
Was This Post Helpful? 0
  • +
  • -

#6 ShotokanDeity  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 13-September 07

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

Posted 17 September 2007 - 01: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.
Was This Post Helpful? 0
  • +
  • -

#7 Bench  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 944
  • View blog
  • Posts: 2,464
  • Joined: 20-August 07

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

Posted 18 September 2007 - 06:47 AM

View PostShotokanDeity, on 17 Sep, 2007 - 09:21 PM, said:

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

	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.
Was This Post Helpful? 0
  • +
  • -

#8 ShotokanDeity  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 96
  • Joined: 13-September 07

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

Posted 18 September 2007 - 07:50 AM

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?

			/*  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 ^_^
Was This Post Helpful? 0
  • +
  • -

#9 mario_dfm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 10
  • Joined: 23-September 07

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

Posted 26 September 2007 - 09:20 AM

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...

#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();
}


Was This Post Helpful? 1

#10 Codegamer  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 127
  • Joined: 04-May 08

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

Posted 15 May 2008 - 02:19 AM

That's a long code lol!


Use this:

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


Was This Post Helpful? 0
  • +
  • -

#11 piotrekpepe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-May 09

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

Posted 12 May 2009 - 02:50 PM

View PostCodegamer, on 15 May, 2008 - 01:19 AM, said:

That's a long code lol!


Use this:

#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 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#12 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

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

Posted 13 May 2009 - 04:00 AM

View Postpiotrekpepe, on 12 May, 2009 - 01:50 PM, said:

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.cprogramm...l/lesson10.html
Was This Post Helpful? 0
  • +
  • -

#13 piotrekpepe  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 12-May 09

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

Posted 13 May 2009 - 05:33 AM

But I do not know how to put all what I have (calendar, for example 2009) to *.txt
Was This Post Helpful? 0
  • +
  • -

#14 janotte  Icon User is offline

  • code > sword
  • member icon

Reputation: 991
  • View blog
  • Posts: 5,141
  • Joined: 28-September 06

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

Posted 13 May 2009 - 06:06 AM

View Postpiotrekpepe, on 13 May, 2009 - 04:33 AM, said:

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.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1