DetermineElapsedTime has only two parameters - both pointers to const MyTime.
DetermineElapsedTime must not modify the contents of either structure.
DetermineElapsedTime must not declare any pointers other than its two parameters.
DetermineElapsedTime must return a pointer to a MyTime structure containing the elapsed time.
Use no external variables, including external structure variables. (The structure template definition is not a variable and typically should be defined externally.)
Use no dynamic storage allocation.
Do no scanning of user input or printing of any kind inside the DetermineElapsedTime function.
Use military time: 23:59:59 is 1 second before midnight; 00:00:00 is midnight; 12:00:00 is noon.
If the second time is less than or equal to the first, the second time is for the next day.
You may (if desired) first convert all times to seconds inside DetermineElapsedTime.
Write a test function that does the following:
prompts the user to input two times, each in the format: hours:minutes:seconds
inputs these values directly into two time structures
calls DetermineElapsedTime and prints the elapsed time result it provides
Beware of potential integer overflow during multiplication!
Returning a pointer to an automatic object is always wrong, as is dereferencing an uninitialized pointer.
#include<iostream>
using namespace std;
const int hourConv = 3600; // used to get total hours from total seconds
const int minConv = 60;
struct MyTime {
int hours, minutes, seconds;
};
MyTime determineElapsedTime(MyTime const &time1, MyTime const &time2)
{
long timeOneSec = time1.hours*hourConv + time1.minutes*minConv + time1.seconds;
long timeTwoSec = time2.hours*hourConv + time2.minutes*minConv + time2.seconds;
long ans = timeTwoSec - timeOneSec;
cout << ans;
MyTime *timeDiff = new MyTime;
timeDiff->hours = ans / hourConv;
timeDiff.minutes = ans % hourConv / minConv;
timeDiff.seconds = ans % hourConv % minConv;
return timeDiff;
}
int main()
{
MyTime determineElapsedTime(MyTime const &time1, MyTime const &time2);
}