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