printing number of days in a month

I write this program to print the number of days for the month when the year and the month enters. This is not giving any result and i think that problem is with calling functions. what wrong with this??


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include<iostream>
#include<conio.h>
#include<string>

using namespace std;

int leap_year(int year);
string days(string mounth);
string feb(string fab1);



void main(){

	int year;
	
Last edited on
Worked for me.

http://s28.postimg.org/sbg6fa8nx/days.png

Windows 7 64-bit.
Compiled with GCC.

Probably wouldn't work if I tried October or November, though. ;-)
Last edited on
Did it worked for February, leap and non-years ??
Nope. There are a couple of reasons for that.

First, you're passing a string into the feb function. Then treating it as an integer. This needs to be converted otherwise you're not working with what you think. To see what I mean, print out the year you're passing in at the start of the function. There are a number of ways to do this, including stoi (c++11) or stringstreams. Alternatively, you could take integer input for the year, rather than a string.

Second, I'm not convinced the leap year algorithm is correct. It should be something like (pseudo):
1
2
3
4
5
6
7
8
9
10
11
if year is evenly divisible by four
{
   return true
}

if year is evenly divisible by 100 &&  year is evenly divisible by 400
{
   return true
}

return false
Last edited on
I don't understand how to pass "leap_year" function output to "feb" function.
Could you show me how to do that?
I have a very little knowledge about passing values between functions.


I wrote the this algorithm in the above program.
"
if year is divisible by 400 then
year is a leap year
else if year is divisible by 100 then
year is not a leap year
else if year is divisible by 4 then
year is a leap year
else
year is not a leap year
"

Is this incorrect ?



@tharindu11, iHutch105
Your leap year algorithms are incorrect.

http://www.wikihow.com/Calculate-Leap-Years

The number of days in February is 28 + is_leap_year( year ). You don't have to "pass" the leap year output to the function, just call it in the function.

That means that the days in February function should take an argument: the current year.


Hope this helps.
@iHitch105 Your algorithm is incorrect. Ex.: 1900 is not a leap year. Your algorithm would say that it is.

@tharindu11 The algorithm you have is correct. The code you posted originally is not. Change your leap year function to return a bool instead of an int. Lay out your code in a manner similar to how you've expressed the algorithm. Notice that I've preserved the algorithm you've typed out in the form of comments.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
bool leap_year(int year)
{
    //if year is divisible by 400 then
    if (year % 400 == 0)
    {
        //year is a leap year
        return true;
    }
    //else if year is divisible by 100 then
    else if (year % 100 == 0)
    {
        //year is not a leap year
        return false;
    }
    //else if year is divisible by 4 then
    else if (year % 4 == 0)
    {
        //year is a leap year
        return true;
    }
    else
    {
        //year is not a leap year
        return false;
    }
}


The problem overall that you're seeing is that you cannot determine the number of days in each month given only the month to work with. You must also know the year. Right now you get a year from the user but you don't pass it to any of your functions. I think that the function 'days' should take a month and a year, then return an int which represents the number of days in the month. The 'days' function would call 'leap_year' in the February case to determine whether to return 28 or 29.
Last edited on
Ah, apologies.

I misinterpreted the information from here: http://www.timeanddate.com/date/leapyear.html
Don't feel bad. I make plenty of stupid mistakes too. Most recent:
http://www.cplusplus.com/forum/general/120094/#msg653451
Topic archived. No new replies allowed.