1. ## loop help

So I have to write a function to determine the number of days that have passed since 1/1/1753 and whatever target year the user inputs. I've already included the algorithm to determine if a year is a leap year or not, but I'm still not sure how to write the loop part of my program.

here's what I have so far: my isLeapYear function and main, which prompts the user for a target year.
Code:
bool isLeapYear()
{
int year;
if (year % 400 == 0)
return true;
else if (year % 100 == 0)
return false;
else if (year % 4 ==0)
return true;
else
return false;
}

int main()
{
int numDays;
int year;
cout << "Year: " << endl;
cin >> year;
cout << "Number of days; " << numDays;
return numDays;
}
I don't expect the exact answers, but I do need help understanding how to write a loop and how to include a loop here.

2. wrote it out of my head

Code:
//directives here

bool isLeapYear(int year)
{
if (year % 400 == 0)
return true;
else if (year % 100 == 0)
return false;
else if (year % 4 ==0)
return true;
else
return false;
}

//main declaration and other code here
int days = 0;
for (; year >= 1973; year--)
{
(isLeapYear(year)) ? (days += 366) : (days += 365); //short version of if else statement
}

3. can you walk me through that please?

4. Originally Posted by akahizzle
can you walk me through that please?
hope this helps

Code:
#include <iostream>
using namespace std;
bool isLeapYear(int year)
{
//instead of declaring year in here
//you should just pass it as a parameter so that it checks that year
//and returns the result
if (year % 400 == 0)
return true;
else if (year % 100 == 0)
return false;
else if (year % 4 ==0)
return true;
else
return false;
}

int countDays(int year)
{
//pass year here again.
int days = 0; //this will hold the total amount of days
/*the fot loop below goes like this
There is not first parameter because we are already passing year initialized with it's value
The loop will run until year is not equal to or greater than 1973
on each spin/cycle or execution of the for loop year is decremented by 1
*/
for (; year >= 1973; year--)
{
//if the year is a leap year then 366 is added to the total of days
(isLeapYear(year)) ? (days += 366) : (days += 365); //short version of if else statement
/*
OR
if (isLeapYear(year) == true)
days = days + 366; //or days += 366
else
days = days + 365; //or days += 365
*/
}
return days //return the amount of days to main
}
int main()
{
int numDays;
int year;
cout << "Year: " << endl;
cin >> year;
numDays = countDays(year); //function call to calculate days
cout << "Number of days; " << numDays;
return 0; //always return 0 at the end of your main
}