C++ output date in number of days per year

  • Context: C/C++ 
  • Thread starter Thread starter SqrachMasda
  • Start date Start date
  • Tags Tags
    C++ Output Per Year
Click For Summary

Discussion Overview

The discussion revolves around calculating the number of days from the start of the year to a given date in C++. Participants explore how to account for leap years and the structure of the code needed to achieve this calculation, including the use of arrays and control structures.

Discussion Character

  • Technical explanation
  • Homework-related
  • Debate/contested

Main Points Raised

  • One participant outlines the conditions for determining leap years, noting that a year is a leap year if it is divisible by 4, except for years divisible by 100 unless they are also divisible by 400.
  • Another participant provides a code snippet that initializes an array for days in each month, adjusting for leap years based on the previously mentioned conditions.
  • A participant expresses uncertainty about how to properly implement the code, asking for clarification on variable declarations and initializations.
  • Some participants suggest referring to standard C or C++ books and online tutorials for guidance, although one participant expresses dissatisfaction with their current resources.
  • There is a light-hearted comment regarding the legality of a URL format mentioned in the discussion.

Areas of Agreement / Disagreement

Participants generally agree on the approach to determining leap years and the structure of the code, but there is no consensus on the best resources for learning or the clarity of the existing materials.

Contextual Notes

Some participants express uncertainty about variable naming and initialization, indicating a potential gap in understanding basic programming concepts. The discussion does not resolve these uncertainties.

SqrachMasda
Messages
42
Reaction score
0
for example
user puts in the month as 1-12
the day for whatever month
and the year, which has to include leapyears
leap years only occur when the year is divisible by 4 y%4==o or if divisible by 400 but NOT 100, so 2000 is a leap year but not 2100

so if somebody puts in month 1 day 1 and 2005 it will say 1
if it's month 12 and day is 31 and year is 2005 it will say 365
but if it was 2004 it would say 366
month 3, day 10, year 2005, would be 31+28+10 or just 69

can anybody do this
i guess it needs boolean stuff for the leap year
and a few switch and case things for the rest

i'm not feeling it though

i got one to kind of work with if statements but not correctly, and it was long
 
Technology news on Phys.org
Code:
if (year%4 == 0 && !(year%100 == 0 && year%400 != 0))
    DaysPerMonth = {31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
else
    DaysPerMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

for (i=0; i<month - 1; i++)
    ndays += DaysPerMonth[i];

ndays += day;
 
Well put :)
 
and what am i putting in right above that (i've been doing this for less than a month so bear with me)
i don't know what exactly i am calling what
int days, etc.?
 
pick up a standard C OR C++ book. there are tutorials about such things usual in those or there are websites...sorry I can't remember C++tutorials.com cplus.com i think are some...
 
yeah i got a book, but it's no good, or it's too good
 
neurocomp2003 said:
pick up a standard C OR C++ book. there are tutorials about such things usual in those or there are websites...sorry I can't remember C++tutorials.com cplus.com i think are some...

heh I don't think that's a legal URL format, only alpha-numerical chars are allowed :-p
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
2K
  • · Replies 9 ·
Replies
9
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 7 ·
Replies
7
Views
7K
  • · Replies 2 ·
Replies
2
Views
12K
Replies
2
Views
12K
  • · Replies 10 ·
Replies
10
Views
3K
Replies
2
Views
2K
  • · Replies 4 ·
Replies
4
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K