 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);
}

Edited 6 Years Ago by potato4610: n/a

First of all your program should be corrected to this.It got compilation errors.

#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);
}

>> DetermineElapsedTime has only two parameters - both pointers to const MyTime.
can't you see man it does.

MyTime determineElapsedTime(MyTime const &time1, MyTime const &time2)

>>  DetermineElapsedTime must not modify the contents of either structure.
when you use the `const` it won't.

>>  DetermineElapsedTime must not declare any pointers other than its two parameters.
The place your code violate this is here.

MyTime *timeDiff = new MyTime;

what you can do is .

MyTime timeDiff ;

>>  DetermineElapsedTime must return a pointer to a MyTime structure containing the elapsed time.
Then what you need to do is change the function to this.

MyTime* determineElapsedTime(MyTime const&time1,MyTime const &time2)
{

and finally, the the bellow requirments overlaps each other toes. I doubt is there
is a magic place to store the data to return?
>> 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.

So what you only can do is pass timeDeff to another variable that creates a new
MyTipe object on the heap and return it's pointer.Then you can return that pointer.
Then you didn't break the rule inside the function, but outside the function you
break the rule.
Ex

MyTime* CopyAndReturnPtr(const &MyTime _time)
{
 MyTime * _return = new MyTime(_time);
 return _return;
}

Then in the return you can use like this.

return CopyAndReturnPtr(timeDiff);

is this is the met your requirement?

What do you mean by? Where should I place it?:

MyTime* CopyAndReturnPtr(const &MyTime _time)
{
 MyTime * _return = new MyTime(_time);
 return _return;
}

and

return CopyAndReturnPtr(timeDiff);

Does ^^^ replace the return statement in the code above it?


Here is the code so far with most of your edits to it. Thank you so much for helping me out :)

#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;
    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);
}
This article has been dead for over six months. Start a new discussion instead.