I'm working on a project for a class I'm taking.
I'm trying take a date from a user, I want to calculate how many days away that is, from todays date (date of entry), then identify based on the number of days which one of 2 programs the user will fall under.
I doubt this is what your instructor is looking for. However if you have aC++11 compiler/library available, here is how you can do it. It uses the new get_time and put_time manipulators in <iomanip> for the date I/O, specifying "%m/%d/%Y" for the input format which appears to be your preferred format. Then it uses <chrono> do convert a double number of seconds into an int number of days.
#include <iostream>
#include <iomanip>
#include <chrono>
template <class To, class Rep, class Period>
To
round_up(const std::chrono::duration<Rep, Period>& d)
{
To t = std::chrono::duration_cast<To>(d);
if (t < d)
++t;
return t;
}
int main()
{
typedef std::chrono::duration<int, std::ratio<86400>> days;
typedef std::chrono::duration<double> dsecs;
// Input separation date
std::cout << "Enter separation date [mm/dd/yyyy]:";
std::tm sep_tm = {0};
while (true)
{
std::cin >> std::get_time(&sep_tm, "%m/%d/%Y");
if (std::cin)
break;
std::cout << "\nInvalid format. Please enter separation date using[mm/dd/yyyy]:";
sep_tm = std::tm{0};
std::cin.clear();
std::cin.ignore(1, '\n');
}
// show off a little
std::cout << "You entered a separation date of " << std:
ut_time(&sep_tm, "%A, %b. %e, %Y\n");
// get difference from current time in double seconds
double secs = std::difftime(std::mktime(&sep_tm), std::time(nullptr));
// convert double seconds to int days
days d = round_up<days>(dsecs(secs));
// Print it out
switch (d.count())
{
case 0:
std::cout << "That is today.\n";
break;
case -1:
std::cout << "That was yesterday.\n";
break;
case 1:
std::cout << "That is tomorrow.\n";
break;
default:
if (d < days(0))
std::cout << "That was " << d.count() << " days ago.\n";
else
std::cout << "That is in " << d.count() << " days.\n";
break;
}
}