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

Dani AI

Generated

Two quick corrections to the approach in the thread: the function signature must accept two pointers to const MyTime (not references), it must not do I/O inside the function, it must not allocate with new, and it must not declare pointer variables other than the two parameters. That rules out returning a pointer to an automatic (stack) object and rules out heap allocation. ’s versions printed inside the function and used heap allocation; suggested heap allocation which violates the "no dynamic storage" rule. Given the constraints, the usual, simple solution is to use a function-local static MyTime as the single return buffer.

The implementation below follows the rules: two parameters (pointers to const), no extra pointer declarations, no dynamic allocation, no globals, no I/O, and it correctly handles the “next day” case (second time <= first). It also uses 64-bit arithmetic for the intermediate second counts to avoid any multiplication overflow on narrow platforms.

MyTime* DetermineElapsedTime(const MyTime* t1, const MyTime* t2)
{
    static MyTime result; // single, persistent buffer (not dynamic, not global)
    long long s1 = (long long)t1->hours * 3600 + (long long)t1->minutes * 60 + t1->seconds;
    long long s2 = (long long)t2->hours * 3600 + (long long)t2->minutes * 60 + t2->seconds;
    if (s2 <= s1) s2 += 24LL * 3600LL; // treat second time as next day when <= first
    long long diff = s2 - s1;
    result.hours   = int(diff / 3600);
    result.minutes = int((diff % 3600) / 60);
    result.seconds = int(diff % 60);
    return &result;
}

Notes and gotchas: a static return buffer is not reentrant and is overwritten by each call, so if you need concurrent calls or to keep multiple results, have the caller provide storage (change the API) or return by value (preferred in modern C++). Validate caller-supplied times (hours 0–23, minutes/seconds 0–59) before calling the function. Remove any cout from DetermineElapsedTime — do all I/O in the caller.

Recommended Answers

All 2 Replies

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);
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.