using loops

So I am trying to write a program that will display the number of days since 1/1/1753. I am not entirely sure how to do this other that to use a loop which I am not too familiar with. What I have so far is below which is very little I know I am just not sure which type of loop to use. I heard that "for" is used for counting so is that what I would be using here? And also how would I write a function to take into account a leap year.

Thanks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int daysYear()
{
  for

}

int main()
{
   int year;

   cout	<< "Year : ";
   cin	>> year;



   return 0;
}

closed account (Lv0f92yv)
Probably have a look at http://www.cplusplus.com/reference/clibrary/ctime/

That library provides several functions that aid in dealing with time, and if converted properly, dates (and distances between them in terms of time).
You don't need a loop for that, and you might not even need the input.

http://cplusplus.com/reference/clibrary/ctime/time/
That function returns the number of seconds since 1/1/1970. You can convert that value to days and add 365.25 * (1970-1753) to that value.

However, if you want an input, just subtract 1753 from the year given, multiply by 365.25, and add the number of days passed since the start of the year.

A for loop is... really just a glorified while loop.

EDIT: Woo, two posts in the same minute.

-Albatross
Last edited on
well I want the user to be able to input a year and then it will return how many days have passed since 1753.

so it would look like this (the 2004 would be the input from the user)

Year: 2004
Number of days: 91675

You're trying to get days from years... the most you can do with this is the number of days since some fixed time in one year and some fixed time within this year.

However, my above general solution should work. Just omit the final step.

-Albatross
Last edited on
So now I have this but I want to loop the number of years from 1753 to the target year(the user's input)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int daysYear()
{
   int year;
   int days;

   days =  year - 1753 * 365;

   return year;
}

int main()
{
   int year;
   int days;

   cout << "Year : ";
   cin  >> year;

   cout << "Number of days: " << days;

   return 0;
}
Why are you insisting on using a loop? There's no need to use one ever here.

EDIT: Is this a homework project perchance?

-Albatross
Last edited on
I want to understand how to use loops because that is what we are learning in class right now and I don't really understand it. This isn't my exact homework assignment but the one I have to do is similar to it.
Oh. In that case:
http://cplusplus.com/doc/tutorial/control/

Scroll down a bit.

-Albatross
I still don't think i understand the concept. This is what I have now and it returns the same number over no matter the input.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int daysYear()
{
   int year;
   int days;

   while (days =  year - 1753 * 365);

   return days;
}

int main()
{
   int year;
   int days;

   cout << "Year : ";
   cin  >> year;

   cout << "Number of days: " << days;

   return 0;
}
That just goes to show you don't need a loop there.

I'm pretty sure the tutorial explained explicitly what a while loop is and how to use it, but I'll try to explain it anyway.

A while loop executes a piece of code in {} while the condition in () evaluates to true. That said, a while loop has the form:
1
2
while (bool expression) {code;}
while (bool expression) single_statement;


-Albatross
Topic archived. No new replies allowed.