Code:
#include <iostream>
#include <string>
#include <climits>
using std::cin;
using std::cout;
using std::numeric_limits;
using std::streamsize;
using std::string;
using std::endl;
//Optional function: depends on IDE. Remove if not needed. void wait_for_enter();
//Additional helper. Remove if not needed. void reset_istream();
//-- Define a day, hour and minute in terms of seconds. "static" is optional right now. static const unsigned SECONDS_IN_DAY = 86400;
static const unsigned SECONDS_IN_HOUR = 3600;
static const unsigned SECONDS_IN_MINUTES = 60;
int main(int argc, char *argv[])
{
unsigned TotalSeconds;
//Get input data.
cout << "Enter Total amount of Seconds: ";
cin >> TotalSeconds;
// Using the following formula: // Days = TotalSeconds / SECONDS_IN_DAY // Hours = (TotalSeconds % SECONDS_IN_DAY) / SECONDS_IN_HOUR // Minutes = ((TotalSeconds % SECONDS_IN_DAY) % SECONDS_IN_HOUR) / SECONDS_IN_MINUTES // Seconds = ((TotalSeconds % SECONDS_IN_DAY) % SECONDS_IN_HOUR) % SECONDS_IN_MINUTES unsigned Days = TotalSeconds / SECONDS_IN_DAY;
unsigned RemainingSeconds = TotalSeconds % SECONDS_IN_DAY;
unsigned Hours = RemainingSeconds / SECONDS_IN_HOUR;
RemainingSeconds %= SECONDS_IN_HOUR;
unsigned Minutes = RemainingSeconds / SECONDS_IN_MINUTES;
unsigned Seconds = RemainingSeconds % SECONDS_IN_MINUTES;
//Output results.
cout<<endl;
cout<<"\t\t\t"<<TotalSeconds<<" seconds equals:"<<endl;
cout<<"\t\t\t"<<Days<<" days"<<endl;
cout<<"\t\t\t"<<Hours<< " hours"<<endl;
cout<<"\t\t\t"<<Minutes<<" minutes"<<endl;
cout<<"\t\t\t"<<Seconds<<" seconds"<<endl;
cout<<endl;
reset_istream();
wait_for_enter();
return 0;
}
//--------------------------------------------------- void reset_istream()
{
if(cin.eof())
{
cin.clear();
}
else
{
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
}
}
//--------------------------------------------------- void wait_for_enter()
{
cout << "press <enter> to continue...";
// Reset failstate, just in case.
cin.clear();
string line;
getline( cin, line);
}