| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
Programing a Day Counter, need help!!
Alright I'm programing a simple day counter here where the user enters 2 years
ex) 2001 and 2004 now the program is suposed to figure out how many days are between those 2 years *note* have to use a for-loop only. heres what I have set up.. for (year=year; year<=yearTwo; year++) I honestly have no idea what formula to use or what to, any help is appreciated, thanks! |
|
#2
|
|||
|
|||
|
Code:
long CalcNumDays( int nStartYear, int nEndYear )
{
if( nStartYear >= nEndYear )
return 0;
long lNumDays(0);
int nYearDifference = nEndYear - nStartYear;
for( int i(0); i < nYearDifference; ++i )
lNumDays += 365;
return lNumDays;
}
|
|
#3
|
|||
|
|||
|
Don't forget leap years :-)
Years evenly divisible by 4 are leap years, with the exception of centurial years that are not evenly divisible by 400 Alright, my coding is VERY rusty. But I'm taking more C++ this semester so I better brush up fast. Please critique my code for this...I know there is an elegant way. I HATE big nested if else loops. There is a way to do this nicely. Code:
long CalcNumDays( int nStartYear, int nEndYear ){
long days(0);
int year(365),leapYear(366);
for(int i(nStartYear);i<nEndYear; i++){
if(i%4==0){
if(i%100==0){
if(i%400==0)
days+=year;
else
days+=leapYear;
{
else
days+=leapYear;
}
else
days+=year;
}
return days;
}
|
|
#4
|
|||
|
|||
|
sorry to post this...but: how the hell do you write code with spaces here?. I've tried to do so, and I get all lines of code align to the left (without the classic TAB). Whenever I use the -> button, the code gets all messy.....I'm just trying to know this, in order to be able to help people here more efitiently.
Thanks.... Anibal. PS: look at this!! void bla(){ asdfasdf; } |
|
#5
|
|||
|
|||
|
I write in textpad, then copy and past it in. I think tabs are translated to spaces.
Quote:
|
|
#6
|
|||
|
|||
|
Thanks....I'll give it a try!...lets see
function bla(){ return 0 } Damn...it didn't work!!! |
![]() |
| Viewing: Dev Articles Community Forums > Programming > C/C++ Help > Programing a Day Counter, need help!! |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|