Time Calculator

This programming challenge is from an initial C++ programming course I took in Fall 2007. The Chapter (4) was titled “Making Decisions”.

I post these old academic challenges for a few reasons. One is to demonstrate my programming learning, experience, and progression. Another reason is to make sure my code and solutions are indexed by search engines, so that other beginning programmers may get help if they need it.

4.7: Write a program that asks the user to enter a number of 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.

There are 3,600 seconds in an hour. If the number of seconds entered by the user is greater than or equal to 3,600, the program should display the number of hours in that many seconds.

There are 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.

My solution:

/* Chapter 4 Challenge 7
   Written by Kirk Hingsberger
   October 2, 2007
   Time Calculator
*/

#include <iostream>
using namespace std;

int main()
{
  int seconds;
  float minutes, hours, days;

  cout << "Please enter a number of seconds." << endl;
  cin >> seconds;

  if (seconds >= 86400)
    {
      days = seconds / 86400.0;
      cout << "You entered " << seconds << ", which equals "
	   << days << " days." << endl;
    }

  else if (seconds >= 3600)
    {
      hours = seconds / 3600.0;
      cout << "You entered " << seconds << ", which equals "
	   << hours << " hours." << endl;
    }
  else if (seconds >= 60)
    {
      minutes = seconds / 60.0;
      cout << "You entered " << seconds << ", which equals "
	   << minutes << " minutes." << endl;
    }
  else
    cout << "You entered " << seconds << " seconds." << endl;

  return 0;
}

Here is the capture script I turned in for grading:

Script started on Tue 02 Oct 2007 12:06:37 PM MDT
$ cat chap4chlg7.cc
/* Chapter 4 Challenge 7
   Written by Kirk Hingsberger
   October 2, 2007
   Time Calculator
*/

#include <iostream>
using namespace std;

int main()
{
  int seconds;
  float minutes, hours, days;

  cout << "Please enter a number of seconds." << endl;
  cin >> seconds;

  if (seconds >= 86400)
    {
      days = seconds / 86400.0;
      cout << "You entered " << seconds << ", which equals "
	   << days << " days." << endl;
    }

  else if (seconds >= 3600)
    {
      hours = seconds / 3600.0;
      cout << "You entered " << seconds << ", which equals "
	   << hours << " hours." << endl;
    }
  else if (seconds >= 60)
    {
      minutes = seconds / 60.0;
      cout << "You entered " << seconds << ", which equals "
	   << minutes << " minutes." << endl;
    }
  else
    cout << "You entered " << seconds << " seconds." << endl;

  return 0;
}
$ g++ -o chap4chlg7 chap4chlg7.cc -s
$ chap4chlg7
Please enter a number of seconds.
59
You entered 59 seconds.
$ chap4chlg7
Please enter a number of seconds.
62
You entered 62, which equals 1.03333 minutes.
$ chap4chlg7
Please enter a number of seconds.
3599
You entered 3599, which equals 59.9833 minutes.
$ chap4chlg7
Please enter a number of seconds.
3604
You entered 3604, which equals 1.00111 hours.
$ chap4chlg7
Please enter a number of seconds.
86399
You entered 86399, which equals 23.9997 hours.
$ chap4chlg7
Please enter a number of seconds.
86401
You entered 86401, which equals 1.00001 days.
$ chap4chlg7
Please enter a number of seconds.
123456789
You entered 123456789, which equals 1428.9 days.
$ exit
exit

Script done on Tue 02 Oct 2007 12:07:33 PM MDT

I later went back and expanded my solution to output in more formats at each level:

/* Chapter 4 Challenge 7 (Expanded Solution)
   Written by Kirk Hingsberger
   October 2, 2007
   Time Calculator
*/

#include <iomanip>
#include <iostream>
using namespace std;

int main()
{
  int seconds;
  int minutes, hours, days;

  cout << "Please enter a number of seconds." << endl;
  cin >> seconds;

  if (seconds >= 86400)
    {
      days = seconds / 86400;
      cout << "Number of days: " << setw(8) << days << endl;
      seconds = seconds % 86400;
      hours = seconds / 3600;
      cout << "Number of hours: " << setw(7) << hours << endl;
      seconds = seconds % 3600;
      minutes = seconds / 60;
      cout << "Number of minutes: " << setw(5) << minutes << endl;
      seconds = seconds % 60;
      cout << "Number of seconds: " << setw(5) << seconds << endl;
    }
  else if (seconds >= 3600)
    {
      hours = seconds / 3600;
      cout << "Number of hours: " << setw(7) << hours << endl;
      seconds = seconds % 3600;
      minutes = seconds / 60;
      cout << "Number of minutes: " << setw(5) << minutes << endl;
      seconds = seconds % 60;
      cout << "Number of seconds: " << setw(5) << seconds << endl;
    }
  else if (seconds >= 60)
    {
      minutes = seconds / 60;
      cout << "Number of minutes: " << setw(5) << minutes << endl;
      seconds = seconds % 60;
      cout << "Number of seconds: " << setw(5) << seconds << endl;
    }
  else
    {
      cout << "Number of seconds: " << setw(5) << seconds << endl;
    }

  return 0;
}
  • Share/Bookmark




Leave a Reply