User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C++ section within the Software Development category of DaniWeb, a massive community of 392,083 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,962 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C++ advertiser:
Views: 2584 | Replies: 14 | Solved
Reply
Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Help with Creating a Yearly Calendar

  #1  
Jan 28th, 2007
Hi all. Eventually my program needs to prompt you to enter a year between 1800-2007 and then it will print off the calendar for that year. In order to do this I must create functions that determine what day of the week Jan 1 was that year, and a function that prints a header for each month and a function that prints off the days of that month. For now, I just said the first day of each month was a Wednesday and that every month has 30 days. Before I figure out how to return the correct amount of days for each month and throw leap years into the equation, I would like help with a much simpler portion of my program. I can't get my program to print a different header for each month. On the very bottom of my program, where I have: [code] void Print_Month_Head(int m) [code\n] if I just type [code] cout << "\n January Sun Mon Tue Wed Thu Fri Sat\n"; [code\n] My program will print a header for January on top of every month. So I tried implementing a for loop for 1<m<12 with an 'if' statement that lists a different cout prompt for every value of m, but when i run the program it just lists the header for january an infinite amount of times. My entire code is listed below, any help I can receive would be great, thanks!

#include <iostream>;
#include <iomanip>;

using std::cin;
using std::cout;
using std::endl;
using std::setw;

int First_Day_Of_Month(int y, int m);
int Number_Days_Of_Month(int y, int m);
void Print_Version();
void Print_Head(int y);
void Print_Month(int y, int m);
void Print_Month_Head(int m);


void main ()
{
    Print_Version();
        int year;
        cin >> year;

    Print_Head(year);
    for(int i=1; i<=12; i++){
        Print_Month(year, i);
    }
    cout << "bye";
}

void Print_Version()
{
    cout << "Welcome \n";
}

void Print_Head(int y)
{
    cout << "                         " << y << endl;
    cout << "==================================================\n";
}

void Print_Month(int y, int m)
{
    Print_Month_Head(m);
    int firstday, number_days;
    firstday = First_Day_Of_Month(y,m);
    number_days = Number_Days_Of_Month(y,m);
    cout << "            ";
    for (int k=0; k<firstday; k++)
        cout << "     ";
    for (int i = 1; i<number_days; i++){
        cout << setw(5) << i;
        if ((i + firstday)%7 == 0){
            cout << endl;
            cout << "            ";
        }
    }
}

int First_Day_Of_Month(int y, int m)
{
    return 2;
}

int Number_Days_Of_Month(int y, int m)
{
    return 30;
}

void Print_Month_Head(int m)
{    
    for (int i = 1; i<=12; i++)
    {
        if (i = 1)
        {
            cout << "\n January      Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 2)
        {
            cout << "\n February     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 3)
        {
            cout << "\n March        Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 4)
        {
            cout << "\n April        Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 5)
        {
            cout << "\n May          Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 6)
        {
            cout << "\n June         Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 7)
        {
            cout << "\n July         Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 8)
        {
            cout << "\n August       Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 9)
        {
            cout << "\n September    Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 10)
        {
            cout << "\n October      Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else if (i = 11)
        {
            cout << "\n November     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
        else
        {
            cout << "\n December     Sun  Mon  Tue  Wed  Thu  Fri  Sat\n";
        }
    
    }


}
Last edited by WaltP : Jan 29th, 2007 at 2:24 am. Reason: Fixed code tags -- please use the PREVIEW button
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Rep Power: 2
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: Help with Creating a Yearly Calendar

  #2  
Jan 28th, 2007
Okay, first thing's first. Print_Month_Head shouldn't be looping. You only want one header, so all you need to do is test m once and print whatever. That fixes the problem that you'll get after you solve the problem that you have now.

The problem that you have now is an infinite loop. I'm sure you're surprised to hear that. The reason you have an infinite loop is that every test in the if chain for Print_Month_Head doesn't do a comparison. It does an assignment. You use = to set a value and == to test a value. Or as I like to say, once to set it, twice to make sure.

If you change = to == and remove the loop in Print_Month_Head, you'll have something that looks more like a calendar.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote  
Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Re: Help with Creating a Yearly Calendar

  #3  
Jan 28th, 2007
Thanks! Yes I could tell it was an infinite loop and figured it had something to do with the for loop. So i took out the for loop and added all the '=='. Now I am trying to determine the number of days to print off per month. I believe I got every month all set except for February (because of leap years). What I was thinking of doing was making another function:

  1. bool IsLeapYear(int y)
  2. {
  3. if (the year is divisible by 400)
  4. it is a leap year;
  5. else if (the year is divisible by 4, but not 100)
  6. it is a leap year;
  7. else
  8. it is not a leap year;

But I am unclear as to how I actually program this function. I know that I can then do something like
  1. if IsLeapYear(y) == 1
  2. return 30;
  3. else
  4. return 29;

Here is my updated code thus far:

  1. #include <iostream>;
  2. #include <iomanip>;
  3.  
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. using std::setw;
  8.  
  9. int First_Day_Of_Month(int y, int m);
  10. int Number_Days_Of_Month(int y, int m);
  11. void Print_Version();
  12. void Print_Head(int y);
  13. void Print_Month(int y, int m);
  14. void Print_Month_Head(int m);
  15.  
  16.  
  17. void main ()
  18. {
  19. Print_Version();
  20. int year;
  21. cin >> year;
  22.  
  23. Print_Head(year);
  24. for(int i=1; i<=12; i++){
  25. Print_Month(year, i);
  26. }
  27. cout << "bye";
  28. }
  29.  
  30. void Print_Version()
  31. {
  32. cout << "Welcome \n";
  33. }
  34.  
  35. void Print_Head(int y)
  36. {
  37. cout << " " << y << endl;
  38. cout << "==================================================\n";
  39. }
  40.  
  41. void Print_Month(int y, int m)
  42. {
  43. Print_Month_Head(m);
  44. int firstday, number_days;
  45. firstday = First_Day_Of_Month(y,m);
  46. number_days = Number_Days_Of_Month(y,m);
  47. cout << " ";
  48. for (int k=0; k<firstday; k++)
  49. cout << " ";
  50. for (int i = 1; i<number_days; i++){
  51. cout << setw(5) << i;
  52. if ((i + firstday)%7 == 0){
  53. cout << endl;
  54. cout << " ";
  55. }
  56. }
  57. }
  58.  
  59. int First_Day_Of_Month(int y, int m)
  60. {
  61. return 2;
  62. }
  63.  
  64. int Number_Days_Of_Month(int y, int m)
  65. {
  66. if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
  67. {
  68. return 32;
  69. }
  70. else if (m == 4 || m == 6 || m == 9 || m == 11)
  71. {
  72. return 31;
  73. }
  74. else
  75. return 29;
  76. }
  77.  
  78. bool IsLeapYear(int y)
  79. {
  80.  
  81. void Print_Month_Head(int m)
  82. {
  83.  
  84.  
  85. if (m == 1)
  86. {
  87. cout << "\n January Sun Mon Tue Wed Thu Fri Sat\n";
  88. }
  89. else if (m == 2)
  90. {
  91. cout << "\n February Sun Mon Tue Wed Thu Fri Sat\n";
  92. }
  93. else if (m == 3)
  94. {
  95. cout << "\n March Sun Mon Tue Wed Thu Fri Sat\n";
  96. }
  97. else if (m == 4)
  98. {
  99. cout << "\n April Sun Mon Tue Wed Thu Fri Sat\n";
  100. }
  101. else if (m == 5)
  102. {
  103. cout << "\n May Sun Mon Tue Wed Thu Fri Sat\n";
  104. }
  105. else if (m == 6)
  106. {
  107. cout << "\n June Sun Mon Tue Wed Thu Fri Sat\n";
  108. }
  109. else if (m == 7)
  110. {
  111. cout << "\n July Sun Mon Tue Wed Thu Fri Sat\n";
  112. }
  113. else if (m == 8)
  114. {
  115. cout << "\n August Sun Mon Tue Wed Thu Fri Sat\n";
  116. }
  117. else if (m == 9)
  118. {
  119. cout << "\n September Sun Mon Tue Wed Thu Fri Sat\n";
  120. }
  121. else if (m == 10)
  122. {
  123. cout << "\n October Sun Mon Tue Wed Thu Fri Sat\n";
  124. }
  125. else if (m == 11)
  126. {
  127. cout << "\n November Sun Mon Tue Wed Thu Fri Sat\n";
  128. }
  129. else
  130. {
  131. cout << "\n December Sun Mon Tue Wed Thu Fri Sat\n";
  132. }
  133.  
  134.  
  135.  
  136.  
  137. }
Last edited by raydogg57 : Jan 28th, 2007 at 5:29 pm.
Reply With Quote  
Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Re: Help with Creating a Yearly Calendar

  #4  
Jan 28th, 2007
I just realized why I had to put an extra day for each month in my code before. It was because in the main function i<number_days needed to be changed to <=number_days. So I fixed that and all the numbers in the Number_of_Days function. But I still need help with this boolean function problem, thanks!
Reply With Quote  
Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Re: Help with Creating a Yearly Calendar

  #5  
Jan 28th, 2007
I created a boolean function listed below:

  1. bool IsLeapYear(int y)
  2. {
  3. if (y%400 == 0)
  4. {
  5. return 1;
  6. }
  7. else if (y%4 == 0 && y%100 != 0)
  8. {
  9. return 1;
  10. }
  11. else
  12. return 0;
  13. }

I then updated my code for Number_of_days as listed below:

  1. int Number_Days_Of_Month(int y, int m)
  2. {
  3. if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
  4. {
  5. return 31;
  6. }
  7. else if (m == 4 || m == 6 || m == 9 || m == 11)
  8. {
  9. return 30;
  10. }
  11. else if (IsLeapYear)
  12. {
  13. return 29;
  14. }
  15. else
  16. {
  17. return 28;
  18. }
  19.  
  20. }

But when I run my program, no matter what year I type, February always has 29 days. Why would this be? I am reattaching my entire code at the bottom of this message, thanks!!

  1. #include <iostream>;
  2. #include <iomanip>;
  3.  
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. using std::setw;
  8.  
  9. int First_Day_Of_Month(int y, int m);
  10. int Number_Days_Of_Month(int y, int m);
  11. bool IsLeapYear(int y);
  12. void Print_Version();
  13. void Print_Head(int y);
  14. void Print_Month(int y, int m);
  15. void Print_Month_Head(int m);
  16.  
  17.  
  18. void main ()
  19. {
  20. Print_Version();
  21. int year;
  22. cin >> year;
  23.  
  24. Print_Head(year);
  25. for(int i=1; i<=12; i++){
  26. Print_Month(year, i);
  27. }
  28. cout << "\nGoodbye! \n";
  29. }
  30.  
  31. void Print_Version()
  32. {
  33. cout << "Please Enter any Year After 1799 \n";
  34. }
  35.  
  36. void Print_Head(int y)
  37. {
  38. cout << " " << y << endl;
  39. cout << "==================================================\n";
  40. }
  41.  
  42. void Print_Month(int y, int m)
  43. {
  44. Print_Month_Head(m);
  45. int firstday, number_days;
  46. firstday = First_Day_Of_Month(y,m);
  47. number_days = Number_Days_Of_Month(y,m);
  48. cout << " ";
  49. for (int k=0; k<firstday; k++)
  50. cout << " ";
  51. for (int i = 1; i<=number_days; i++){
  52. cout << setw(5) << i;
  53. if ((i + firstday)%7 == 0){
  54. cout << endl;
  55. cout << " ";
  56. }
  57. }
  58. }
  59.  
  60. bool IsLeapYear(int y)
  61. {
  62. if (y%400 == 0)
  63. {
  64. return 1;
  65. }
  66. else if (y%4 == 0 && y%100 != 0)
  67. {
  68. return 1;
  69. }
  70. else
  71. return 0;
  72. }
  73.  
  74. int First_Day_Of_Month(int y, int m)
  75. {
  76. return 2;
  77. }
  78.  
  79. int Number_Days_Of_Month(int y, int m)
  80. {
  81. if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
  82. {
  83. return 31;
  84. }
  85. else if (m == 4 || m == 6 || m == 9 || m == 11)
  86. {
  87. return 30;
  88. }
  89. else if (IsLeapYear)
  90. {
  91. return 29;
  92. }
  93. else
  94. {
  95. return 28;
  96. }
  97.  
  98. }
  99.  
  100.  
  101.  
  102.  
  103.  
  104. void Print_Month_Head(int m)
  105. {
  106.  
  107.  
  108. if (m == 1)
  109. {
  110. cout << "\n January Sun Mon Tue Wed Thu Fri Sat\n";
  111. }
  112. else if (m == 2)
  113. {
  114. cout << "\n February Sun Mon Tue Wed Thu Fri Sat\n";
  115. }
  116. else if (m == 3)
  117. {
  118. cout << "\n March Sun Mon Tue Wed Thu Fri Sat\n";
  119. }
  120. else if (m == 4)
  121. {
  122. cout << "\n April Sun Mon Tue Wed Thu Fri Sat\n";
  123. }
  124. else if (m == 5)
  125. {
  126. cout << "\n May Sun Mon Tue Wed Thu Fri Sat\n";
  127. }
  128. else if (m == 6)
  129. {
  130. cout << "\n June Sun Mon Tue Wed Thu Fri Sat\n";
  131. }
  132. else if (m == 7)
  133. {
  134. cout << "\n July Sun Mon Tue Wed Thu Fri Sat\n";
  135. }
  136. else if (m == 8)
  137. {
  138. cout << "\n August Sun Mon Tue Wed Thu Fri Sat\n";
  139. }
  140. else if (m == 9)
  141. {
  142. cout << "\n September Sun Mon Tue Wed Thu Fri Sat\n";
  143. }
  144. else if (m == 10)
  145. {
  146. cout << "\n October Sun Mon Tue Wed Thu Fri Sat\n";
  147. }
  148. else if (m == 11)
  149. {
  150. cout << "\n November Sun Mon Tue Wed Thu Fri Sat\n";
  151. }
  152. else
  153. {
  154. cout << "\n December Sun Mon Tue Wed Thu Fri Sat\n";
  155. }
  156.  
  157. }
Reply With Quote  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Rep Power: 2
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: Help with Creating a Yearly Calendar

  #6  
Jan 28th, 2007
You aren't calling the function with an argument. This is something your compiler should be wraning you about.
else if (IsLeapYear)
It should be
else if (IsLeapYear( y ))
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote  
Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0
raydogg57 raydogg57 is offline Offline
Light Poster

Re: Help with Creating a Yearly Calendar

  #7  
Jan 28th, 2007
Thanks Ravalon! What I actually did instead was:

  1. int Leapyear;
  2. Leapyear = IsLeapYear(y);
  3. else if (Leapyear == 1)

and the code works just fine! Now the only (and biggest) kink I have left in my code is determining the first day of every month. The information I was given was:

First day of January in 1800 was a Wednesday. Then adding up all the days up to this given year(including extra days from leap years) and finding the remainder of 7. this supposedly helps me determine the day of the week for the first day of every month. I am unclear as to how to type a code for something like this, it seems like it would be a lot of 'if' statements. I am going to try and sit and think about this but I was wondering if you could give me some ideas to work with while developing this, because it is giving me a lot of trouble. Thanks a lot! As always, my code is posted below:

  1. #include <iostream>;
  2. #include <iomanip>;
  3.  
  4. using std::cin;
  5. using std::cout;
  6. using std::endl;
  7. using std::setw;
  8.  
  9. int First_Day_Of_Month(int y, int m);
  10. int Number_Days_Of_Month(int y, int m);
  11. bool IsLeapYear(int y);
  12. void Print_Version();
  13. void Print_Head(int y);
  14. void Print_Month(int y, int m);
  15. void Print_Month_Head(int m);
  16.  
  17.  
  18. void main ()
  19. {
  20. Print_Version();
  21. int year;
  22. cin >> year;
  23.  
  24. Print_Head(year);
  25. for(int i=1; i<=12; i++){
  26. Print_Month(year, i);
  27. }
  28. cout << "\nGoodbye! \n";
  29. }
  30.  
  31.  
  32. //Functions
  33.  
  34. void Print_Version()
  35. {
  36. cout << "Please Enter any Year After 1799 \n";
  37. }
  38.  
  39. void Print_Head(int y)
  40. {
  41. cout << " " << y << endl;
  42. cout << "==================================================\n";
  43. }
  44.  
  45. void Print_Month(int y, int m)
  46. {
  47. Print_Month_Head(m);
  48. int firstday, number_days;
  49. firstday = First_Day_Of_Month(y,m);
  50. number_days = Number_Days_Of_Month(y,m);
  51. cout << " ";
  52. for (int k=0; k<firstday; k++)
  53. cout << " ";
  54. for (int i = 1; i<=number_days; i++){
  55. cout << setw(5) << i;
  56. if ((i + firstday)%7 == 0){
  57. cout << endl;
  58. cout << " ";
  59. }
  60. }
  61. }
  62.  
  63. bool IsLeapYear(int y)
  64. {
  65. if (y%400 == 0)
  66. {
  67. return 1;
  68. }
  69. else if (y%4 == 0 && y%100 != 0)
  70. {
  71. return 1;
  72. }
  73. else
  74. return 0;
  75. }
  76.  
  77. int First_Day_Of_Month(int y, int m)
  78. {
  79. return 2;
  80. }
  81.  
  82. int Number_Days_Of_Month(int y, int m)
  83. {
  84. int Leapyear;
  85. Leapyear = IsLeapYear(y);
  86. if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12)
  87. {
  88. return 31;
  89. }
  90. else if (m == 4 || m == 6 || m == 9 || m == 11)
  91. {
  92. return 30;
  93. }
  94. else if (Leapyear == 1)
  95. {
  96. return 29;
  97. }
  98. else
  99. {
  100. return 28;
  101. }
  102.  
  103. }
  104.  
  105. void Print_Month_Head(int m)
  106. {
  107.  
  108. if (m == 1)
  109. {
  110. cout << "\n January Sun Mon Tue Wed Thu Fri Sat\n";
  111. }
  112. else if (m == 2)
  113. {
  114. cout << "\n February Sun Mon Tue Wed Thu Fri Sat\n";
  115. }
  116. else if (m == 3)
  117. {
  118. cout << "\n March Sun Mon Tue Wed Thu Fri Sat\n";
  119. }
  120. else if (m == 4)
  121. {
  122. cout << "\n April Sun Mon Tue Wed Thu Fri Sat\n";
  123. }
  124. else if (m == 5)
  125. {
  126. cout << "\n May Sun Mon Tue Wed Thu Fri Sat\n";
  127. }
  128. else if (m == 6)
  129. {
  130. cout << "\n June Sun Mon Tue Wed Thu Fri Sat\n";
  131. }
  132. else if (m == 7)
  133. {
  134. cout << "\n July Sun Mon Tue Wed Thu Fri Sat\n";
  135. }
  136. else if (m == 8)
  137. {
  138. cout << "\n August Sun Mon Tue Wed Thu Fri Sat\n";
  139. }
  140. else if (m == 9)
  141. {
  142. cout << "\n September Sun Mon Tue Wed Thu Fri Sat\n";
  143. }
  144. else if (m == 10)
  145. {
  146. cout << "\n October Sun Mon Tue Wed Thu Fri Sat\n";
  147. }
  148. else if (m == 11)
  149. {
  150. cout << "\n November Sun Mon Tue Wed Thu Fri Sat\n";
  151. }
  152. else
  153. {
  154. cout << "\n December Sun Mon Tue Wed Thu Fri Sat\n";
  155. }
  156.  
  157. }
Reply With Quote  
Join Date: Dec 2006
Posts: 209
Reputation: Ravalon is on a distinguished road 
Rep Power: 2
Solved Threads: 15
Ravalon's Avatar
Ravalon Ravalon is offline Offline
Posting Whiz in Training

Re: Help with Creating a Yearly Calendar

  #8  
Jan 28th, 2007
Originally Posted by raydogg57 View Post
First day of January in 1800 was a Wednesday. Then adding up all the days up to this given year(including extra days from leap years) and finding the remainder of 7. this supposedly helps me determine the day of the week for the first day of every month. I am unclear as to how to type a code for something like this, it seems like it would be a lot of 'if' statements. I am going to try and sit and think about this but I was wondering if you could give me some ideas to work with while developing this, because it is giving me a lot of trouble. Thanks a lot!
You might like this one. yy is the year, mm is the month, dd is the day.

y = yy - (14 - mm) / 12
m = mm + 12 * ((14 - mm) / 12) - 2

Now take a deeeeeep breath.

d = (dd + y + y / 4 - y / 100 + y / 400 + (31 * m / 12) % 7

Whew! And now d contains the numeric value of the day, starting at 0 with Sunday. No if statements needed. My best advice is never to take the obvious solution without first trying to think of other ways around a problem. If you're not a mathematics buff, something like this also strikes me as a good problem for a lookup table. You can precalculate by hand the first day of each month for a millenia or so and put them all in an array. That's less than 100 values, so it's still a viable option.
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
Reply With Quote  
Join Date: Jan 2007
Posts: 41
Reputation: raydogg57 is an unknown quantity at this point 
Rep Power: 2
Solved Threads: 0