1,105,578 Community Members

calculating days in a month

Member Avatar
fugnut
Junior Poster in Training
54 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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;
}
Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
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?

Member Avatar
fugnut
Junior Poster in Training
54 posts since Feb 2010
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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.....

Member Avatar
WaltP
Posting Sage w/ dash of thyme
9,363 posts since May 2006
Reputation Points: 2,905 [?]
Q&As Helped to Solve: 1,151 [?]
Skill Endorsements: 45 [?]
Team Colleague
 
0
 

Reread my post.

Member Avatar
Fbody
Posting Maven
2,845 posts since Oct 2009
Reputation Points: 682 [?]
Q&As Helped to Solve: 393 [?]
Skill Endorsements: 7 [?]
Featured
 
0
 

Read this.
Now, take a look at the first line of your function...

int calcDays (int)

Anything missing?

Member Avatar
evandrix
Newbie Poster
1 post since Jan 2013
Reputation Points: 13 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

if (isLeapYear == 0): surely you mean if (isLeapYear)?

Member Avatar
Miorfs
Newbie Poster
15 posts since Nov 2012
Reputation Points: -1 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

I think value number0fdays is not pass to the function calcdays

Member Avatar
abishur
Newbie Poster
1 post since Jan 2013
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

I know the OP did this YEARS ago, but here's how the function should look for anyone who wants to do this:

int calcDays(int month, int year)
{
int Days;


if (month == 4 || month == 6 || month == 9 || month == 11) Days = 30;

else if (month == 2) {
    bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    if (isLeapYear) Days = 29;
    else Days = 28;
}
else Days = 31;

return Days;
}
Member Avatar
deceptikon
Eternally Awesome
4,687 posts since Jan 2012
Reputation Points: 1,341 [?]
Q&As Helped to Solve: 688 [?]
Skill Endorsements: 104 [?]
Administrator
Featured
 
0
 

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';
        }
    }
}
You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
Tags Related to this Article