Friday, November 18, 2016

Programming Challenge 4.7 - Time Calculator

/* Time Calculator - This program asks the user to enter a number in seconds.
   There are:
  
   * 60 seconds in a minute. If the number of seconds entered by the user is
     greater than or equal to 60, the program should display the number of
     minutes in that many seconds.
   
   * 3,600 seconds in an hour. If the number of seconds entered by the user
     is greater than or equal to 3600, the program should display the number
     of hours in that many seconds.
   
   * 86,400 seconds in a day. If the number of seconds entered by the user is
     greater than or equal to 86,400, the program should display the number of
     days in that many seconds. */

#include "Utility.h"

int main()
{
    /* Hold constants for seconds in minute, hour and day */
    const double SECONDS_IN_MINUTE = 60;
    const double SECONDS_IN_HOUR = 3600;
    const double SECONDS_IN_DAY = 86400;

    /* Hold minutes, hours and days */
    double minutes = 0,
           hours = 0,
           days = 0;

    /* Hold choice and seconds */
    int seconds;

    /* Ask the user for the input of a value in seconds */
    cout << "\t\tTime Converter - Enter a time in seconds and i will convert\n"
         << "it to minutes or days.\n\n"
         << "Enter seconds: ";
    cin >> seconds;

    /* Calculations */
    minutes = seconds / SECONDS_IN_MINUTE;
    hours = seconds / SECONDS_IN_HOUR;
    days = seconds / SECONDS_IN_DAY;

    /* Format the output */
    cout << showpoint << fixed << setprecision(2);

    if (seconds >= SECONDS_IN_MINUTE && seconds <= SECONDS_IN_HOUR)
    {   
        if (seconds == SECONDS_IN_HOUR)
        {
            cout << seconds << " seconds is exactly " << hours << " hour\n";
        }
        else
        {
            cout << seconds << " seconds is " << minutes << " minute(s)\n";
        }
    }

    else if (seconds >= SECONDS_IN_HOUR && seconds <= SECONDS_IN_DAY)
    {
        if (seconds == SECONDS_IN_DAY)
        {
           
            cout << seconds << " seconds is exactly " << days << " day\n";
        }
        else
        {
            cout << seconds << " seconds is " << hours << " hour(s)\n";
        }
    }

    else if (seconds >= SECONDS_IN_DAY)
    {
        cout << seconds << " seconds is " << days << " day(s)\n";
    }
    else
    {
        cout << "Enter a valid amount of seconds (60 or above!)\n";
    }

    pauseSystem();
    return 0;
}

No comments:

Post a Comment