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>
usingnamespace std;
int leap_year(int year);
string days(string mounth);
string feb(string fab1);
void main(){
int year;
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
{
returntrue
}
if year is evenly divisible by 100 && year is evenly divisible by 400
{
returntrue
}
returnfalse
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
"
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.
@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.
bool leap_year(int year)
{
//if year is divisible by 400 then
if (year % 400 == 0)
{
//year is a leap year
returntrue;
}
//else if year is divisible by 100 then
elseif (year % 100 == 0)
{
//year is not a leap year
returnfalse;
}
//else if year is divisible by 4 then
elseif (year % 4 == 0)
{
//year is a leap year
returntrue;
}
else
{
//year is not a leap year
returnfalse;
}
}
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.