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!
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 */ /* -------------------------------- */
/* 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; }
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
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
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
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
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.
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; } }
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");
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
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.