468,491 Members | 2,069 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,491 developers. It's quick & easy.

Help with printing a calendar

hi i have this code to print out a calendar based on the year and month entered by user!!!!!
but im getting only the heading information ???can someone take a look at it and tell me whats wrong??TANX
my code so far is::

Full code removed per Posting Guidelines
Nov 17 '07 #1
20 5125
i need to write a program that prints the 12 months calandar given the weakday which should be the 1st of January for the years of 2001 and 2004( a leap year)!!! i know the test for leap year and i have written the FUN to determine the number of the days of months.
i just dont know how to print the calandar like
S M T W TH F SU
1 2 3 4 5 6
7 8 9 10 11 12 13
and so on....and the the of the next month should follow the end of previous month

... im sure i have to use the reference variables but im not sure how??
can you guys give me any ideas?
TANX
Nov 26 '07 #2
Ganon11
3,652 Expert 2GB
It should just be a matter of deciding how many days each month has, and then printing them in alignment (check out the setw() function in the iomanip header file for this). The first half is a mixture of simple logic and constants, the second half is experiment and 'making it look pretty'.
Nov 26 '07 #3
It should just be a matter of deciding how many days each month has, and then printing them in alignment (check out the setw() function in the iomanip header file for this). The first half is a mixture of simple logic and constants, the second half is experiment and 'making it look pretty'.
i know how many days esch month has. i dont know hoe to put the first day of January on the day of the week that user chooses?
like when a user inputes 2 (when Sunday is 1 and saturday is 7) the 1st day of january should be on monday!!!!!any ideas?
Nov 26 '07 #4
I think your main( ) needs changes. You need to assign the functions to variables when you are returning values from those functions. Also the code to generate the calendar needs to be appropriately changed. (Like the half written code inside the comment needs to be refined and continued...)
Nov 26 '07 #5
Ganon11
3,652 Expert 2GB
Again, this is a matter of experiment to make it look 'good'. My solution would be to simply print a certain number of spaces until you got under the 'M' spot, and then beginning to print.
Nov 26 '07 #6
sicarie
4,677 Expert Mod 4TB
Alenik1989-

Please do not double post - confine yourself to a single thread for each question you have.

Thanks
Nov 26 '07 #7
actually i didnt write the end of this code (my friend wrote it) where the code is

Full code removed per Posting Guidelines

can anyone please tell me whats the rule of this FUN, i mean whats is the daysofweek ? and what should i do with it?
Nov 27 '07 #8
sicarie
4,677 Expert Mod 4TB
Try stepping through the code line by line. Pass it a value that you think would make sense, and see where the logic takes you. Then pass it a value that you think would not make sense and see where the logic takes you.

As this is your friends code, I'm removing it from your post (due to our Posting Full Code rule in the Posting Guidelines, but if you come across a specific part you don't understand, post that part and your question, as well as the values you put in, what you got back, and why it doesn't make sense to you.
Nov 27 '07 #9
i just need a loop to print out the numbers from function in lines that in each line there will be 7 numbers!!!!!

like
1 2 3 4 5 6 7
8 9 10 11 12 13 14
i got the FUNC to determine the number of days ina month so
Expand|Select|Wrap|Line Numbers
  1. int i=1,j=1
  2. while( i<=FUNC)
  3. {
  4. while( j<=7)
  5. {
  6. if(i<=FUNC)
  7. {
  8. cout<<"   "<<i;
  9. i++
  10. j++
  11. }
  12. else
  13. break;
  14. }
  15. cout<<endl;
  16. j=1
  17. }
  18.  
just wanna know is this loop gonna work??? if not any idea how to write it???!!
please i need ASAP!! TANX
Nov 30 '07 #10
sicarie
4,677 Expert Mod 4TB
just wanna know is this loop gonna work???
I have no idea. Did you compile it?
Nov 30 '07 #11
I have no idea. Did you compile it?
yeh i got it!!!!tanx
Nov 30 '07 #12
i rewrote the code (myself) and now my code is like
Expand|Select|Wrap|Line Numbers
  1. #include<iostream>
  2. #include<iomanip>
  3. using namespace std;
  4. //_____________________Prototypes
  5. int leaptest(int);
  6. int daysofmonth(int,int);
  7. void printcalandar(int,int);
  8. int main()
  9. {
  10. int year,month=1;
  11. cin>>year;
  12. leaptest(year);//leapyearcheck
  13. while (month<=12)
  14. {
  15. daysofmonth(month,year);//# of days in a month
  16. printcalandar (month,year);//printing the calandar
  17. month++;
  18. }
  19. return 0;
  20. }
  21. int leaptest (int year)
  22. {
  23. if((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
  24.   return 1; /* leap */
  25.  else
  26.   return 0; /* no leap */
  27. }
  28. int daysofmonth (int month,int year)
  29. {
  30. int days;
  31. int month2;
  32. month2 = month + 11;
  33. month2 = month2 * 7;
  34. month2 = month2 % 12;
  35. if (month2 < 7)
  36.   days = 31;
  37. else if (month==2)
  38. {
  39. if(leaptest(year)==1)
  40.     days=29;
  41.     else
  42.     days=28;
  43. }
  44. else
  45. days=30;
  46. return days;
  47. }
  48. void printcalandar(int month,int year)
  49. {
  50. int i=1,j=1;
  51. while(i<=daysofmonth(month,year))
  52. {
  53.     while(j<=7)
  54.     {
  55.         if(i<=daysofmonth(month,year))
  56.         {
  57.         cout<<setw(4)<<i;
  58.                   i++;
  59.         j++;
  60.         }
  61.         else 
  62.         break;
  63. }
  64. cout<<endl;
  65. if(i<=daysofmonth(month,year))
  66. j=1;
  67. else
  68. break;
  69. }
  70. cout<<"I="<<i<<"   j="<<j<<endl;
  71. }
this code will print 12 group month's calandar right.
but i want it to be like ...when each month ends the next month starts on the dayof the week that follows the last day of the preceding month!!!
like when january 31 is Monday then February 1st has to be on Thuesday.
Any ideas how??
i need it today pleasssssssssseeee!!!!
TANX
Nov 30 '07 #13
sicarie
4,677 Expert Mod 4TB
I'd create a variable, say 'dayOfWeekYesterday' and store it in there.
Nov 30 '07 #14
I'd create a variable, say 'dayOfWeekYesterday' and store it in there.
im new to programming, so can you be a little more specific?please?
where do i put the variable and how i make the loop to see it?
Dec 1 '07 #15
sicarie
4,677 Expert Mod 4TB
im new to programming, so can you be a little more specific?please?
where do i put the variable and how i make the loop to see it?
I would read up on rules of scope to decide where you should put that variable, but I'd recommend: your program. If you declare it outside your loop, your loop can see it. If you declare it inside your loop, your loop can see it, but every time the loop starts over, it will have an undefined value that you will need to reset.
Dec 1 '07 #16
I would read up on rules of scope to decide where you should put that variable, but I'd recommend: your program. If you declare it outside your loop, your loop can see it. If you declare it inside your loop, your loop can see it, but every time the loop starts over, it will have an undefined value that you will need to reset.
SORRY, but i still dont get it?
what is that variable suppose to do?
please i just have this pronblem with my calandar!!!
(i feel so stupid right now!!!!)
Dec 1 '07 #17
sicarie
4,677 Expert Mod 4TB
SORRY, but i still dont get it?
what is that variable suppose to do?
please i just have this pronblem with my calandar!!!
(i feel so stupid right now!!!!)
The variable holds the day of the week that the month ended with (as this is what you asked just a few posts ago...). Then you initialize the new month with the day after the one held in the variable.
Dec 1 '07 #18
The variable holds the day of the week that the month ended with (as this is what you asked just a few posts ago...). Then you initialize the new month with the day after the one held in the variable.
okay i knew that part but in order to give the variable the day of the week that the month ended with i have to put it the void printcalandar(month,year) function and before the final break right?
so after the function ends the memory location of all variables including this one will dialocated right?
so how a im suppose to initialize the new month with the day after the on in the variobale? (i know i have to add i to that variable but it should be in the functon as well)
Dec 1 '07 #19
sicarie
4,677 Expert Mod 4TB
okay i knew that part but in order to give the variable the day of the week that the month ended with i have to put it the void printcalandar(month,year) function and before the final break right?
so after the function ends the memory location of all variables including this one will dialocated right?
so how a im suppose to initialize the new month with the day after the on in the variobale? (i know i have to add i to that variable but it should be in the functon as well)
So printcalendar() only prints a month?

As it is declared a void, you aren't currently returning anything. You could return the current day of week, and then pass it back to that function.
Dec 1 '07 #20
So printcalendar() only prints a month?

As it is declared a void, you aren't currently returning anything. You could return the current day of week, and then pass it back to that function.
yeh printcalandar is a void function that i have putted in a while loop in main function to just print the 12 months using the # days in month.
Expand|Select|Wrap|Line Numbers
  1. while (month<=12)
  2. {
  3. daysofmonth(month,year);
  4. printcalandar (month,year);
  5. month++;
  6. }
but i cant return a value in void function can i?
you mean i have to change it to a returning value function?
Dec 1 '07 #21

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

10 posts views Thread by Mario | last post: by
reply views Thread by Parthiv Joshi | last post: by
2 posts views Thread by TristaSD | last post: by
2 posts views Thread by Kevin | last post: by
7 posts views Thread by William (Tamarside) | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.