Write a C/C++ program that determines exactly how many hours have passed since a
ID: 3779455 • Letter: W
Question
Write a C/C++ program that determines exactly how many hours have passed since a previous historical event happened. For example, if the user entered 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. No you should take into account the extra days due to leap years and century years and century years that are evenly divided by 4; along with the number of days in each month (i.e. whether it is a 28, 30 or 31 day month). For example, if the user entered 0900 hours (9AM) for the hour, 7 for the month of July, 4 for the day and 1776 for the year, your program would calculate and return of number of hours that have passed since the declaration of independence. Passing of King Tut: 1 May 1355 BC @ 12 midnight Fall of Rome: 10 August 410 AD @ 6 AM St Andrews sighting of the Loch Ness Monster: 14 November 794 AD @ 4 PM Christopher Columbus discovery of Americas: 12 October 1492 @ 2 AM Declaration of Independence: 4 July 1776 @ 9 AMExplanation / Answer
#include <iostream>
#include <ctime>
using namespace std;
int main()
{
int year;
char month;
int hours;
cout << "Please hours: ";
cin >> hours;
cout << "Please year: ";
cin >> year;
cout << "Please month: ";
cin >> month;
cin >> day;
cout << "Please day: ";
if(year % 400 == 0 || (year % 100 != 0 && year % 4 == 0))
{
leap=1;
}
else
leap=2;
if( month='jan' || 'March' || 'May' || 'July' || 'August' || 'October' || 'December' )
{
numberofdays=31;
}
if( month='April' || 'June' || 'September' || 'November' )
{
numberofdays=30;
}
if( month='feb' && leap=1)
{
numberofdays=28;
}
if( month='feb' && leap=2)
{
numberofdays=28;
}
//creating an array
previous date={hours,year,month,day};
// generating for current date
tm *ltm = localtime(&now);
// print various components of tm structure.
cout << "Year" << 1900 + ltm->tm_year<<endl;
cout << "Month: "<< 1 + ltm->tm_mon<< endl;
cout << "Day: "<< ltm->tm_mday << endl;
cout << "Time: "<< 1 + ltm->tm_hour << ":";
cout << 1 + ltm->tm_min << ":";
cout << 1 + ltm->tm_sec << endl;
present date={1 + ltm->tm_hour,1900 + ltm->tm_year,1 + ltm->tm_mon,1 + ltm->tm_hour};
double difference = std::difftime(present date, previous date) / (60 * 60 * 24);
std::cout << "difference = " << difference << " days" << std::endl;
}
output:
Please hours:9
Please year:1776
Please month:7
Please day:4
//
Please hours:2
Please year:1492
Please month:10
Please day:12
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.