•
•
•
•
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
![]() |
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
Rep Power: 2
Solved Threads: 0
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
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.

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.
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
Rep Power: 2
Solved Threads: 0
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:
But I am unclear as to how I actually program this function. I know that I can then do something like
Here is my updated code thus far:
c Syntax (Toggle Plain Text)
bool IsLeapYear(int y) { if (the year is divisible by 400) it is a leap year; else if (the year is divisible by 4, but not 100) it is a leap year; else 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
c Syntax (Toggle Plain Text)
if IsLeapYear(y) == 1 return 30; else return 29;
Here is my updated code thus far:
c Syntax (Toggle Plain Text)
#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) { if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { return 32; } else if (m == 4 || m == 6 || m == 9 || m == 11) { return 31; } else return 29; } bool IsLeapYear(int y) { void Print_Month_Head(int m) { if (m == 1) { cout << "\n January Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 2) { cout << "\n February Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 3) { cout << "\n March Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 4) { cout << "\n April Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 5) { cout << "\n May Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 6) { cout << "\n June Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 7) { cout << "\n July Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 8) { cout << "\n August Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 9) { cout << "\n September Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 10) { cout << "\n October Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 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 raydogg57 : Jan 28th, 2007 at 5:29 pm.
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
Rep Power: 2
Solved Threads: 0
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!
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
Rep Power: 2
Solved Threads: 0
I created a boolean function listed below:
I then updated my code for Number_of_days as listed below:
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!!
c Syntax (Toggle Plain Text)
bool IsLeapYear(int y) { if (y%400 == 0) { return 1; } else if (y%4 == 0 && y%100 != 0) { return 1; } else return 0; }
I then updated my code for Number_of_days as listed below:
c Syntax (Toggle Plain Text)
int Number_Days_Of_Month(int y, int m) { if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { return 31; } else if (m == 4 || m == 6 || m == 9 || m == 11) { return 30; } else if (IsLeapYear) { return 29; } else { return 28; } }
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!!
c Syntax (Toggle Plain Text)
#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); bool IsLeapYear(int y); 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 << "\nGoodbye! \n"; } void Print_Version() { cout << "Please Enter any Year After 1799 \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 << " "; } } } bool IsLeapYear(int y) { if (y%400 == 0) { return 1; } else if (y%4 == 0 && y%100 != 0) { return 1; } else return 0; } int First_Day_Of_Month(int y, int m) { return 2; } int Number_Days_Of_Month(int y, int m) { if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { return 31; } else if (m == 4 || m == 6 || m == 9 || m == 11) { return 30; } else if (IsLeapYear) { return 29; } else { return 28; } } void Print_Month_Head(int m) { if (m == 1) { cout << "\n January Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 2) { cout << "\n February Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 3) { cout << "\n March Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 4) { cout << "\n April Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 5) { cout << "\n May Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 6) { cout << "\n June Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 7) { cout << "\n July Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 8) { cout << "\n August Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 9) { cout << "\n September Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 10) { cout << "\n October Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 11) { cout << "\n November Sun Mon Tue Wed Thu Fri Sat\n"; } else { cout << "\n December Sun Mon Tue Wed Thu Fri Sat\n"; } }
You aren't calling the function with an argument. This is something your compiler should be wraning you about.
It should be
else if (IsLeapYear)
else if (IsLeapYear( y ))
It's hard to be humble when you're as gifted as I am at pretending to be an expert.
•
•
Join Date: Jan 2007
Posts: 41
Reputation:
Rep Power: 2
Solved Threads: 0
Thanks Ravalon! What I actually did instead was:
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:
c Syntax (Toggle Plain Text)
int Leapyear; Leapyear = IsLeapYear(y); 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:
c Syntax (Toggle Plain Text)
#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); bool IsLeapYear(int y); 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 << "\nGoodbye! \n"; } //Functions void Print_Version() { cout << "Please Enter any Year After 1799 \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 << " "; } } } bool IsLeapYear(int y) { if (y%400 == 0) { return 1; } else if (y%4 == 0 && y%100 != 0) { return 1; } else return 0; } int First_Day_Of_Month(int y, int m) { return 2; } int Number_Days_Of_Month(int y, int m) { int Leapyear; Leapyear = IsLeapYear(y); if (m == 1 || m == 3 || m == 5 || m == 7 || m == 8 || m == 10 || m == 12) { return 31; } else if (m == 4 || m == 6 || m == 9 || m == 11) { return 30; } else if (Leapyear == 1) { return 29; } else { return 28; } } void Print_Month_Head(int m) { if (m == 1) { cout << "\n January Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 2) { cout << "\n February Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 3) { cout << "\n March Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 4) { cout << "\n April Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 5) { cout << "\n May Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 6) { cout << "\n June Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 7) { cout << "\n July Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 8) { cout << "\n August Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 9) { cout << "\n September Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 10) { cout << "\n October Sun Mon Tue Wed Thu Fri Sat\n"; } else if (m == 11) { cout << "\n November Sun Mon Tue Wed Thu Fri Sat\n"; } else { cout << "\n December Sun Mon Tue Wed Thu Fri Sat\n"; } }
•
•
•
•
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!
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.

