calculate Elapsed time using functions and header file.

I am writing a code that takes two user inputted times and switches them all to seconds(using the function) gives me the elapsed time and stores it in my header file, then writes to screen elapsed time. I am having trouble in my function with Time1. and Time.2 coming up errors using pointers, struct, and I am sure much other problems. I really need help I am stuck! All is appreciated.
say I enter 12:15:30 for start time and end time 13:30:30 it should output my elapsed time is 1:15:00.

#include <iostream>
#include <fstream>
using namespace std;

struct MyTime { int hours,minutes, seconds; };
int DeterminedElapsedTime(struct Mytime *Time1, struct Mytime *Time2);

int main()
{
struct MyTime Time1;
struct MyTime Time2;

cout << "Input first clocktime and seccond clocktime " << endl;
cin >> Time1.hours >> Time1.minutes >> Time1.seconds >> Time2.hours >> Time2.minutes >> Time2.seconds;
}

int DeterminedElapsedTime(Mytime * Time1, Mytime * Time2)
{
return 0;
}

my function:

int DeterminedElapsedTimestruct ( struct Mytime *Time1, struct Mytime *Time2)
{
double elapsedtime;

Time1 = Time1.hours*(3600) + Time1.minutes*(60) + Time1.seconds;
Time2 = Time2.hours*(3600) + Time2.minutes*(60) + Time2.seconds;
elapsedtime = Time1 - Time2;
return elapsedtime;

and my header file:

struct MyTime { int hours, minutes, seconds; };
Please always use code tags:
http://www.cplusplus.com/articles/z13hAqkS/


I haven't tested this yet, just modified your code :+)


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
#include <fstream>
using namespace std;

struct MyTime { 
unsigned int hours;       //declare one variable per line
unsigned short int minutes;   // unsigned type, we don't want negative values
unsigned short int  seconds; 
};

// Pass values by reference, instead of pointer, struct keyword is not needed MyTime already a type
// Watch spelling C++ is case sensitive
// use const for values we don't change, a reference for the answer we want
// see how I formatted the code when there are multiple parameters

void DeterminedElapsedTime(  const MyTime& Time1,  
                                       const MyTime& Time2, 
                                       double& ElapsedTimeAsSeconds);

int main()
{
 MyTime Time1;
 MyTime Time2;

cout << "Input first clocktime and second clocktime \n";

// this could be dodgy, instructions aren't clear need to input values separated by spaces
cin >> Time1.hours >> Time1.minutes >> Time1.seconds >> Time2.hours >> Time2.minutes >> Time2.seconds;

// do validation of input, minutes and seconds 0 to 59

double ElapsedTimeAsSeconds = 0.0;
//function call doesn't have a return type, or types for the arguments
DeterminedElapsedTime( Time1, Time2, ElapsedTimeAsSeconds);
// need to do something with the answer - Convert to HH:MM:SS ?

return 0;
}

// changed to void return type, the answer we want is a reference
void DeterminedElapsedTimestruct (  const MyTime& Time1,  const MyTime& Time2, double& ElapsedTimeAsSeconds)
{
//delay declaration until you have a value to initialise with
double elapsedtime;

//use different variable names

double Time1AsSeconds = Time1.hours*3600 + Time1.minutes*60 + Time1.seconds;
double Time2AsSeconds = Time2.hours*3600 + Time2.minutes*60 + Time2.seconds;

ElapsedTimeAsSeconds = Time1AsSeconds - Time2AsSeconds;
return elapsedtime;

}

// what header file? Not including any header file, already have this declaration in this file
//and my header file:

struct MyTime { int hours, minutes, seconds; }; 

Last edited on
Thanks I am still getting some errors. (reminder that these are all separate files)
Severity Code Description Project File Line Suppression State
Error C2228 left of '.hours' must have class/struct/union and a few others. Any idea on how to get it to run??
Hi,

If you have your struct in a header file you need to include that file.

..... and a few others.


If you have compiler errors then post them here in full. The code compiles on my system.
Topic archived. No new replies allowed.