We're a community of 1.1M IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,080,548 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

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;
}
7
Contributors
8
Replies
2 Years
Discussion Span
4 Months Ago
Last Updated
10
Views
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
Team Colleague
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

Reread my post.

WaltP
Posting Sage w/ dash of thyme
Team Colleague
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

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

evandrix
Newbie Poster
1 post since Jan 2013
Reputation Points: 13
Solved Threads: 0
Skill Endorsements: 0

I think value number0fdays is not pass to the function calcdays

mior.farhan.9
Newbie Poster
14 posts since Nov 2012
Reputation Points: -1
Solved Threads: 2
Skill Endorsements: 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;
}
abishur
Newbie Poster
1 post since Jan 2013
Reputation Points: 0
Solved Threads: 0
Skill Endorsements: 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';
        }
    }
}
deceptikon
Challenge Accepted
Administrator
3,499 posts since Jan 2012
Reputation Points: 822
Solved Threads: 481
Skill Endorsements: 58

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page generated in 0.0838 seconds using 2.75MB