0

I can't change a lot of things in this code due to certain limitations on the assignment. Here is the code:

#include <iostream>
#include<cstdlib>
#include<cstring>

using namespace std;
struct MyTime { int hours, minutes, seconds; };
int DetermineElapsedTime(const MyTime *t1, const MyTime *t2);
const int hourSeconds = 3600;
const int minSeconds = 60;

int DetermineElapsedTime(const MyTime *t1, const MyTime *t2)
{//problem about static in next line
    static MyTime ((long) (((t2.hours * hourSeconds) + (t2.minutes * minSeconds) +  t2.seconds) -
                   ((t1.hours * hourSeconds) + (t1.minutes * minSeconds) + t1.seconds)));
    return(MyTime);
}

It is not the entire thing but I need to somehow calculate the amount of time from primary input to the other. I will need to used setfill too.

Anyway, does anyone know how to fix the error about needing a primary expression before static?

2
  • This doesn't make a lot of sense... you're returning a type, MyTime when you are supposed to be returning an int Commented Nov 12, 2012 at 5:57
  • I'm still learning. Any idea why I get the negative vote? Commented Nov 12, 2012 at 6:33

3 Answers 3

2

what you want to write (i think) is more like

MyTime DetermineElapsedTime(const MyTime *t1, const MyTime *t2)
{
    MyTime var = { ...... };
    return var;
}

What was the intention behind the static in that line? That will cause issues if you call this function more than once, since that line will be executed only once during the life time of the program (if you put static), thus gonna give a wrong answer.

Also the return type was off. And you need fix the ->/. as @mux explains

edit: you were using a constuctor syntax, but no constructor is defined for MyTime, I think you need to use MyTime var = {...} syntax instead of MyTime var(...)

Sign up to request clarification or add additional context in comments.

1 Comment

Honestly, it was a hint from a professor. I do not fully understand his ways.
1

There are many problems in this piece of code.

First, you cannot return a struct type, you need to return a variable.

When creating your static variable, you failed to give it a name and use the = sign.

When using pointers you need to use -> instead of .

Now, the logic in your code will give you the difference in seconds, you will then need to convert it to proper hours, minutes and seconds to fill a MyTime variable. Here's an example of how you could compute the time (not tested, just an example):

int difference = (t2->hours * hoursSeconds + t2->minutes * minSeconds + t2->seconds) - (t1->hours * hoursSeconds + t1->minutes * minSeconds + t1->seconds); // this gives you a difference in seconds
int hoursDifference = difference / hoursSeconds; // how many full hours we have
difference -= hoursDifference * hoursSeconds; // remove from total what we just computed
int minsDifference = difference / minsSeconds; // how many full minutes we have
difference -= minsDifference * minsSeconds;
MyTime diff;
diff.hours = hoursDifference;
diff.minutes = minsDifference;
diff.seconds = difference;
return diff;

2 Comments

I knew I would eventually have to convert it back to the proper format. The problem is i will need long type cast then change it back to int
Just follow the formula I gave you and you should be all set
1

you forgot the type and name of the variable, it seems like you want the difference between two MyTime objects, the difference seems to be returned as an int not in a new MyTime:

int t = ((int) (((t2->hours * hourSeconds) + (t2->minutes * minSeconds) +  t2.seconds) - ((t1->hours * hourSeconds) + (t1->minutes * minSeconds) + t1->seconds)));
return(t);

Also, t1 and t2 are pointers so use -> instead of . to access the members, and if you use static the variable will be initialized once and the same value is returned every time.

5 Comments

If I was taught properly I wouldn't be asking these questions. Thank You. My lecture is all talk, no code...
There is no possible conversion from int t to MyTime
@emartel the function doesn't return a MyTime object it returns an int the first line is not a struct initialization syntax, it just computes the difference of the two MyTime objects. if that's your down vote please remove it.
I did remove it, when reviewing the comments I came across Karthik's solution and misread it as the OP function
@emartel it's okay, both answers are valid you could either return the difference as an int or in a MyTime structure but in the OP's question an int is computed and returned.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.