d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Yymmdd Function > Yearyear/monthmonth/dayday Function
12Next
Add Reply New Topic New Poll
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 18 2013 12:08pm
I need to write a function that returns the number of days of a month. For example if you type in "930224" it should work but if you put in "930230" it shoudlnt work since february just got 28 days... it should also take leap year into consideration.. but thats rather easy I think..

leap year code
Code
if year is divisible by 400 then
is_leap_year
else if year is divisible by 100 then
not_leap_year
else if year is divisible by 4 then
is_leap_year
else
not_leap_year
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 18 2013 12:28pm
I think Ive found what im looking for..

Code
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;
}


still got a problem I think, I need to use a string to get the value into my function..

This post was edited by bomben on Oct 18 2013 12:55pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Oct 18 2013 12:50pm
heh sounds like something from project euler...
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 18 2013 12:56pm
Quote (Azrad @ Oct 18 2013 07:50pm)
heh sounds like something from project euler...


project euler?
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Oct 18 2013 01:01pm
Quote (bomben @ Oct 18 2013 11:56am)
project euler?


http://projecteuler.net/problem=19
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 18 2013 01:42pm
Quote (Azrad @ Oct 18 2013 08:01pm)


Hmm no, I dont think so..

My problem looks like this


1, Read a personal ID number into a string variable
2, Validate the ID number, check if the ID was written in the correct format (yymmdd-ssss). If the given ID does not match the correct format display an error message and ask the user to type it again. Notice that the prgram should verify the date (yymmdd) given in the ID so it matches an existing date.. for example 930230 should display error and 930224 should be correct..
3, The program should check if the ID number corresponds to a female or a male.. if the last digit in "ssss" is an odd number then its a male otherwise its a female.

The program should end when the first two numbers are 0. (00mmdd-ssss)


Currently I've done a few functions..

Code
void yymmdd()
{
string personnummer;


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;

}

void valid()
{
if personnummer V[i] > V[12]
{
cout << "Error, please type again" << endl;
}
}

void formatcheck()
{

}

void manorwoman()
{

}

void quit()
{

}

int main()
{

}


Edit; No numeric code should be used

This post was edited by bomben on Oct 18 2013 01:44pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Oct 18 2013 02:57pm
Quote (bomben @ Oct 18 2013 12:42pm)
Code
void manorwoman()
whoah there, we run a clean sub forum here!



Quote (bomben @ Oct 18 2013 11:28am)
still got a problem I think, I need to use a string to get the value into my function..

but seriously, I'm not much of a C++ programmer, but maybe this will help you:
Code
std::string input_string="930224";

std::string year_string = input_string.substr (0,2);
int year = atoi(year_string.c_str());

std::string month_string = input_string.substr (2,2);
int month = atoi(month_string.c_str());

std::string day_string = input_string.substr (4,2);
int day = atoi(day_string.c_str());

don't forget to check to make sure 0<month<13

This post was edited by Azrad on Oct 18 2013 02:59pm
Member
Posts: 10,812
Joined: Oct 15 2009
Gold: Locked
Warn: 20%
Oct 18 2013 03:05pm
also it think the following line will be problematic:
Code
bool isLeapYear = (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
since you are receiving a 2 digit year input (I think it would be fine for a 4 digit input).

This post was edited by Azrad on Oct 18 2013 03:11pm
Member
Posts: 5,913
Joined: May 6 2009
Gold: 0.00
Oct 19 2013 04:06am
Code
std::string input_string="930224";

std::string year_string = input_string.substr (0,2);
int year = atoi(year_string.c_str());

std::string month_string = input_string.substr (2,2);
int month = atoi(month_string.c_str());

std::string day_string = input_string.substr (4,2);
int day = atoi(day_string.c_str());


hmm I dont really understand that code...

but im thinking of something like this; the string is a "array" of words correct? and then u should search the slots etc. and the lenght of the "array" should be 11 letters long..
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Oct 19 2013 06:58am
Quote (bomben @ Oct 19 2013 06:06am)
Code
std::string input_string="930224";

std::string year_string = input_string.substr (0,2);
int year = atoi(year_string.c_str());

std::string month_string = input_string.substr (2,2);
int month = atoi(month_string.c_str());

std::string day_string = input_string.substr (4,2);
int day = atoi(day_string.c_str());


hmm I dont really understand that code...

but im thinking of something like this; the string is a "array" of words correct? and then u should search the slots etc. and the lenght of the "array" should be 11 letters long..


where did 11 come from?
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll