A C++ program that determines exactly how many hours have passed since a previou
ID: 3688573 • Letter: A
Question
A C++ program that determines exactly how many hours have passed since a previous historical event happened. for example if the user enterd the hour and date of the historical event (including the time of day in military format, the month in string format, the day of the month in integer format and year in 4 digit format) then your program would calculate the number of hours between then and the present time. Note you should take into account the extra days due to leap years and the numbers of days in a month. for example if the user entered 1400 hours for the hour , July for the month, 4 for the day and 1776 for the year, your program would calculate and return the number of hours that have passed since the declaration of independence. Hint: looking up the Gregorian calendar and leap years on Wikipedia can give some useful information on taking leap years into account
Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
int main()
{
time_t now;
struct tm newyear;
double seconds;
double hr;
time(&now); /* get current time; same as: now = time(NULL) */
newyear = *localtime(&now);
newyear.tm_hour = 1400; newyear.tm_min = 0; newyear.tm_sec = 0;
newyear.tm_mon = 7; newyear.tm_mday = 4; newyear.tm_year = 1776;
seconds = difftime(now,mktime(&newyear));
hr = seconds / (60*60);
cout << hr << " hours since in the current timezone. ";
system("pause");
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.