calculating days in a month
Hello all,
I am working with a program to calculate the days in a given month. We are to use functions and bool. I have the below code, which worked until I tried to create the function. I continue to get an error about "numberOfDays" not being initialized however if I state that numberOfDays = 0, that is all i get in return, 0....any suggestions?
//C++ program to determin the number of days in a given month
//of a given year.
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int calcDays (int);
int main ()
{
int month, year;
int numberOfDays;
cout <<"Month and year, and I will calculate the number of days in that month. ";
cout <<endl;
cout <<"Enter the month (eg. 02 for February): ";
cin >> month;
cout <<"Enter the year (eg. 2010): ";
cin >>year;
calcDays (numberOfDays);
cout <<"Month "<<"Year "<<"# of Days";
cout <<endl;
cout << setfill('0') << setw(2)<<month<<" "<<year<<" "<<numberOfDays;
cout <<endl;
system ("pause");
return 0;
}
int calcDays (int)
{
int month = 0;
int Days;
int year = 0;
if (month == 4 || month == 6 || month == 9 || month == 11)
Days = 30;
else if (month == 02)
{
bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if (isLeapYear == 0)
Days = 29;
else
Days = 28;
}
else
Days = 31;
return Days;
}
fugnut
Junior Poster in Training
54 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
You aren't retrieving the value in main() .
Where does calcDays (numberOfDays); put the value upon return? Do you need to brush up on returning values in you book?
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
I corrected my function to read
int calcDays (int)
{
int month = 0;
int numberOfDays;
int year = 0;
if (month == 4 || month == 6 || month == 9 || month == 11)
numberOfDays = 30;
else if (month == 02)
{
bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
if (isLeapYear == 0)
numberOfDays = 29;
else
numberOfDays = 28;
}
else
numberOfDays = 31;
return numberOfDays;
}
however I still get the error that numberOfDays is not initialized....not sure where to "initialize" it and have it return the correct value.....
fugnut
Junior Poster in Training
54 posts since Feb 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0
WaltP
Posting Sage w/ dash of thyme
11,404 posts since May 2006
Reputation Points: 3,421
Solved Threads: 1,055
Skill Endorsements: 37
Read this.
Now, take a look at the first line of your function...
int calcDays (int)
Anything missing?
Fbody
Posting Maven
2,929 posts since Oct 2009
Reputation Points: 833
Solved Threads: 394
Skill Endorsements: 5
I know the OP did this YEARS ago, but here's how the function should look for anyone who wants to do this:
Since we're talking about other people who want to do this, a table lookup is the recommended solution:
#include <exception>
#include <iostream>
#include <stdexcept>
inline int is_leap(int year)
{
return year > 0 && !(year % 4) && (year % 100 || !(year % 400));
};
int calcDays(int month, int year)
{
static const int monthdays[2][13] = {
{ 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 },
{ 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }
};
if (month < 1 || month > 12) {
throw std::invalid_argument("Invalid month");
}
return monthdays[is_leap(year)][month - 1];
}
int main()
{
std::cout << "\t2000\t2001\n";
for (int i = -1; i < 15; i++) {
try {
std::cout << i << ":\t";
std::cout << calcDays(i, 2000) << '\t' << calcDays(i, 2001) << '\n';
} catch (const std::exception& ex) {
std::cout << ex.what() << '\n';
}
}
}
deceptikon
Challenge Accepted
3,499 posts since Jan 2012
Reputation Points: 822
Solved Threads: 481
Skill Endorsements: 58