0

I am trying to make a Coid-19 self-assessment tool and to determine how long the user should quarantine for the program needs to know their time they were exposed and the time they were given their second shot.

Currently I have exactly what I want my function file to look like but the issue is, how to subtract the stored date from 1/1/2021. I am passing the values from the function with pass by const reference.

//Include proper libraries
#include <iostream>
#include <cstdlib>

using namespace std;

//Include header files
#include "Date.h"
#include "CalcDays.h"

//Define function to calculate the ammount of time between exposure and users second shot
int calcDays(const Date& constRefDate1, const Date& constRefDate2) {

    //Define local var
    int daysDiff1;
    int daysDiff2;

    //Calculate the difference between daysDiff and 1/1/2021
    daysDiff1 = constRefDate1 -  
    daysDiff2 = constRefDate2 - 

    //Return the difference between date exposed and date you have gotten the shot
    return abs(daysDiff1-daysDiff2);

}
Justin s
  • 11
  • 1
  • Have you tried overriding the `Date` class's `operator-`? – cs1349459 Sep 30 '22 at 12:46
  • Per the [CDC guidance](https://www.cdc.gov/media/releases/2021/s1227-isolation-quarantine-guidance.html) you also need to know which vaccination the 2nd dose was (6 months for mRNA or 2 months for J&J). It's also not clear if your code should use abs(d1 - d2). – jarmod Sep 30 '22 at 12:51
  • currently the Date class is used for just storing user input and outputting it to the user in the MM/DD/YYYY format. I do I imagine i could store 1/1/2021 into the Date class and then attempt to subtract them. Not sure what additional function I would need. I did see a function to subtract strings in C++. – Justin s Sep 30 '22 at 12:52
  • @jarmod our professor only tasked us with finding the time between second shot and the users time of exposure. Once we know they are at or greater than 14 days we can declare them fully vaccinated. Else the user isn't and must quarantine for 10 days per CDC, 5 days if fully vaccinated. – Justin s Sep 30 '22 at 12:59
  • `subtract strings in C++` what? If you want to subtract dates then you must subtract dates, string functions are useless here. You need to look at each dates's year, month, day (in that order) and then compute accordingly (sum each years's days, each month's days, etc). It is easy but it is not trivial, if you sit and think about it for a minute you will reach the solution before anyone does it for you. – ichramm Sep 30 '22 at 13:06
  • Does this answer your question? [Determining the difference between dates](https://stackoverflow.com/questions/9987562/determining-the-difference-between-dates) – Andrei Vukolov Sep 30 '22 at 16:17
  • Unfortunately not, we are not allowed to use any Date libraries or any functions not taught to us currently. I have two instances that I am trying to reference that store that month day and year. I have it written as Date datePositive; and then my setters datePositive.setMonth(m); and so forth to store in the users input. – Justin s Sep 30 '22 at 17:13

1 Answers1

0

Well, since you cannot use any library functions designed for this purpose, you will have to implement the functionality you need by yourself.

Since your current function is meant to return the difference (in days) between two arbitrary Dates, my own preference would be to call it operator-, as suggested by cs1349459, and put it in Date.h/cpp. Somewhat like this:

// Date.h

class Date {
    // Existing stuff...

public:    
    int daysSince01012021() const;
};

int operator-(Date const&, Date const&);
// Date.cpp

int Date::daysSince01012021() const
{
    // Compare our year/month/day with 2021/1/1 and return the 
    // difference in days
}

int operator-(Date const& lhs, Date const& rhs)
{
    return lhs.daysSince01012021() - rhs.daysSince01012021();
}

That is just my preference, as it loosely mimics the interface of the std::chrono classes. If you have other guidelines to follow, that is also fine.

As for the actual code that should go into this daysSince01012021 function, the question mentioned by Andrei Vukolov does have answers with "hand-rolled" algorithms you could study. I could probably come up with one, but just like you I would have to determine how robust it should be (should it account for dates before 1/1/2021 and leap years?), as well as test my code, as it will likely be sensitive to "off by one" errors. I would not want to take that experience away from you! ;)

sigma
  • 2,758
  • 1
  • 14
  • 18