0

I'm a student and I was wondering if anybody can help me debug one of my functions. For this program the user is suppose to input two dates in the format mm/dd/yy between the years of 1972-2071 and the program is suppose to output the difference between those two dates. All of my functions work except for the function that calculates the amount of days from 1/1/72. This is the way our professor would like us to do it with no extra features. Just a beginner version with a lot of if else statements and for loops.

int ChangeToNumber(int m, int d, int y)
{
    int total=0;

    if(y<=71) //if the year is between 2000-2071
    {
        y+=28;

        for(int i=0; i<y; i++)
        {
            if(!LeapYear(y))
            {
                total+=365;
            }
            else
            {
                total+=366;
            }
        }
    }
    else //if the year is between 1972-1999
    {
        for(int i=72; i<y; i++)
        {
            if(!LeapYear(y))
            {
                total+=365;
            }
            else
            {
                total+=366;
            }
        }
    }


    for(int i=1; i<m; i++)
    {
        total+=DaysInMonth(m, y);
    }

    total += d;
    return total;
}
Ben Wainwright
  • 4,224
  • 1
  • 18
  • 36
kdf
  • 1
  • 1
  • 1
  • 1
    you will need to add a more complete sample of code. By example the methods `LeapYear` and `DaysInMonth` are unknown to readers – meJustAndrew Oct 09 '16 at 19:31
  • Cheap hack: `(year + 28) %100` will save you some trouble and code duplication – user4581301 Oct 09 '16 at 19:38
  • 1
  • [duplicate](http://stackoverflow.com/questions/9987562/determining-the-difference-between-dates) – amanuel2 Oct 09 '16 at 19:41
  • Since d isn't changed at all throughout the function, but is needed for the return value, simply set total equal to d as the first thing you do. You should also represent years as 4 digits instead of 2. Adding 28 to values below or equal to 71 is a very hacky way of doing things and makes code less readable. An alternative way of calculating years is simply doing y - 1972. – Zypps987 Oct 09 '16 at 20:01
  • Also, your leapyear function adds days even if the input year is the same as 1972. Remember you are calculating a difference in 99 years, not 100. Always consider being off by 1 whenever you're debugging your code. – Zypps987 Oct 09 '16 at 20:18

2 Answers2

3

You can use std::difftime to help you in some percents.

no extra features:

http://en.cppreference.com/w/cpp/chrono/c/difftime

#include <iostream>
#include <ctime>

int main()
{
    struct std::tm a = {0,0,0,24,5,104}; /* June 24, 2004 */
    struct std::tm b = {0,0,0,5,6,104}; /* July 5, 2004 */
    std::time_t x = std::mktime(&a);
    std::time_t y = std::mktime(&b);
    if ( x != (std::time_t)(-1) && y != (std::time_t)(-1) )
    {
        double difference = std::difftime(y, x) / (60 * 60 * 24);
        std::cout << std::ctime(&x);
        std::cout << std::ctime(&y);
        std::cout << "difference = " << difference << " days" << std::endl;
    }
    return 0;
}

extra features:

#include "boost/date_time/gregorian/gregorian_types.hpp"

using namespace boost::gregorian;
date date1(2012, Apr, 2);
date date2(2003, Feb, 2);
long difference = (date1 - date2).days();
CyberGuy
  • 2,783
  • 1
  • 21
  • 31
0

I think you are looking for basic gregorian calendar algorithms:

http://howardhinnant.github.io/date_algorithms.html

ChangeToNumber looks a lot like http://howardhinnant.github.io/date_algorithms.html#days_from_civil and the algorithms explained in detail there are valid over a much greater range than the paltry 1972-2071.

The syntax at this calendrical algorithms link is not very good (though the algorithms are very good). In C++ you can achieve quite nice syntax with the very efficient algorithms under the hood with a library like this: https://howardhinnant.github.io/date/date.html

This program:

#include "chrono_io.h"
#include "date.h"
#include <iostream>

int
main()
{
    using namespace date;
    std::cout << sys_days{2016_y/oct/9} - sys_days{2016_y/jan/1} << '\n';
}

Outputs:

282[86400]s

Which means that if 2016-01-01 is day 0, then 2016-10-09 is day 282

Howard Hinnant
  • 206,506
  • 52
  • 449
  • 577