C Program which results in this algorithm down below KEEP I
C++ Program which results in this algorithm down below : (KEEP IT SIMPLE)
Time Calculator
Write a program that asks the user to enter a number of seconds, and converts that number into the xxd hh:mm:ss military format. The minimum value is 00d 00:00:00 and the maximum time is 99d 23:59:59. Thus, your program must validate the acceptable range of values for seconds, displaying an appropriate message for out-of-range values. The program must loop for as long as the user wish, exiting with an entry of ‘x’ or ‘X’ at the appropriate prompt.
o There are 86,400 seconds in a day [0-86399]. Hence, the integer quotient of input_seconds/86400 is the number of days; and, the remainder of input_seconds modulus 86400 is the time. A similar calculation must be performed to obtain the time in hours, minutes and seconds. o For instance, if a data-entry is 150500, then:
days = 150500 / 86400 = 1, remainder = 64100 hours = 64100 / 3600 = 17, remainder = 2900 minutes = 2900 / 60 = 48 seconds = 2900 modulus 60 = 20. Thus, the output will be formatted as
01d 17:48:20
Hints:
1. Think of the appropriate data type(s) to use 2. Use compound conditions –with logical operators- to validate the data-entry 3. The program must loop for as long as the user wishes, stopping at the entry of ‘x’ or ‘X’
Solution
/**
C++ program that prompts user to enter number of seconds to which
program converts to days, hours,minutes and seconds to console.
If user enters invalid seconds then re prompt untilo user
enters a valid seconds. The program will stop
if user enters x or X to stop
*/
//secondsconvert.cpp
//header file
#include<iostream>
using namespace std;
//main method
int main()
{
int MIN_SECONDS=0;
int MAX_SECONDS=8639999;
int userSeconds;
int second,minute,hour,days;
char userChoice=\'y\';
bool repeat=true;
//continue until user enters x or X to stop
while(repeat)
{
do
{
//prompt for seconds
cout<<\"Enter number of seconds [\"<<MIN_SECONDS<<\",\"<<MAX_SECONDS<<\"] :\";
cin>>userSeconds;
if(userSeconds<MIN_SECONDS || userSeconds>MAX_SECONDS)
cout<<\"Please enter valid seconds .\"<<endl;
}while(userSeconds<MIN_SECONDS || userSeconds>MAX_SECONDS);
//calculate days
days = userSeconds / 86400;
// Obtain the total seconds since the midnight, Jan 1, 1970
int totalSeconds = userSeconds%86400;
// calculate the seconds in totalSeconds %60
second = totalSeconds % 60;
// Get remaining minutes
int totalMinutes = totalSeconds / 60;
// calculate the minutes in totalSeconds %60
minute = totalMinutes % 60;
// Obtain the total hours from totalMinutes/60
int totalHours = totalMinutes / 60;
// calculate the hours from remianing totalHours
hour = (int)(totalHours % 24);
cout<<\"formatted time : \"<<endl;
if(days<10)
cout << days << \"0d\" << hour << \":\" << minute << \":\"<<second<<endl;
else
cout << days << \"d\" << hour << \":\" << minute << \":\"<<second<<endl;
cout<<\"Enter x or X to quit or any key to continue\";
cin>>userChoice;
if(userChoice==\'x\' || userChoice==\'X\')
repeat=false;
}
system(\"pause\");
return 0;
}
Sample Output:

