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?
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):
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 anint
not in a newMyTime
:Also,
t1
andt2
are pointers so use->
instead of.
to access the members, and if you usestatic
the variable will be initialized once and the same value is returned every time.what you want to write (i think) is more like
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 explainsedit: you were using a constuctor syntax, but no constructor is defined for MyTime, I think you need to use
MyTime var = {...}
syntax instead ofMyTime var(...)