Hello!
Working on a code where we need to create a class TimeDiff that takes Hours/Minutes/Seconds and compares it to the last time the clock struck noon(e.g. 12,0,0). Time entered will be in universal format so (0-23). If the time is invalid then the method will return -1 and print an error message and exit otherwise the program will return the number of seconds since the previous noon.
I need a validatetime method that validates the hour,minute, second and needs to be called by secondssincenoon and needs to be boolean method that returns true if valid, false otherwise.
EG (9,0,0) will output 75600 seconds
(24,0,0) will print out Invalid time entered!
(13,23,55) will print out 5035.
Here is my code. At first I kept getting a negative number for my sum and then positive number for the difference.
I couldn't get the program to exit.
I did some edits and now I am getting an error at the Main.cpp at the functions. Not too sure where I am going wrong. If someone can point me in the right direction that would be awesome :) I am trying to really grasp the material this time around.
Here is my Header File
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
#ifndef TIMEDIFF_H
#define TIMEDIFF_H
class TimeDiff
{
public:
int getentHour();
int getentMinute();
int getentSec();
int getsumSec();
bool validateTime(int, int, int, int);
int secondsSinceNoon(int, int, int, int);
private:
int entHour;
int entMinute;
int entSec;
int sumofsec;
};
#endif
|
Here is my TimeDiff.CPP
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
|
#include <iostream>
#include "TimeDiff.h"
using std::cout;
using std::cin;
using std::endl;
int TimeDiff::getentHour()
{
return entHour;
}
int TimeDiff::getentMinute()
{
return entMinute;
}
int TimeDiff::getentSec()
{
return entSec;
}
int TimeDiff::getsumSec()
{
//total in seconds of input
return (entSec + (entMinute * 60) + (entHour * 60 * 60));
}
bool TimeDiff::validateTime(int entHour, int entMinute, int entSec, int sumofsec)
{
sumofsec = (entSec + (entMinute * 60) + (entHour * 60 * 60));
if ((entSec + (entMinute * 60) + (entHour * 60 * 60)) == 86400)
{
return true;
cout << "Invalid time entered!! \n";
}
else
{
return false;
}
}
int TimeDiff::secondsSinceNoon(int entHour,int entMinute,int entSec, int sumofsec)
{
bool validateTime();
//noon in seconds
int noon = 43200;
int seconds;
seconds = noon - sumofsec;
return seconds;
}
|
Here is my Main.CPP
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
|
#include <iostream>
#include "TimeDiff.h"
using std::cout;
using std::cin;
int main()
{
TimeDiff timediff;
int entHour;
int entMinute;
int entSec;
int sumofsec;
cout << "--------------Welcome to Seconds CLOCK-------------\n";
cout << "Enter Hour: ";
cin >> entHour;
cout << "Enter Minute: ";
cin >> entMinute;
cout << "Enter Second: ";
cin >> entSec;
timediff.getentHour();
timediff.getentMinute();
timediff.getentSec();
timediff.validateTime(entHour, entMinute, entSec, sumofsec);
timediff.secondsSinceNoon(entHour, entMinute, entSec,sumofsec);
cout << "Sum of seconds = " << timediff.getsumSec() << "\n";
cout << "Seconds since previous noon= " << timediff.secondsSinceNoon(entHour, entMinute, entSec, sumofsec) << "\n";
return 0;
}
|