Time Class to Compare Seconds since Noon

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;
}
Last edited on
validation seem weird. seems like you want to check each item, hours from 0-23, the rest from 0-59. your sum won't work, 0 hour and 153 min is probably valid using that sum approach, and worse, you used == so there are only a FEW exactly invalid times you catch and many that you miss where the numbers are wrong but the sum isnt equal to that magic number.

ok, but assuming you put in good values... I am looking at that now.
what do YOU think line 41 does?
what error are you getting?
Last edited on
Hello mrsduhh,

You need to rethink your code.

Based on what you have you can eliminate the class "TimeDiff" since you never use it. And in the "TimeDiff.cpp" file you can remove all the get functions. The last 2 functions you can remove the "TimeDiff::" and make them regular functions and for "validateTime" you do not use it properly.

1
2
3
4
5
6
7
8
9
10
11
12
int TimeDiff::secondsSinceNoon(int entHour,int entMinute,int entSec, int sumofsec)
{
	bool validateTime();  // <--- Nice prototype, but not complete, and not a function call.
	//noon in seconds
	int noon = 43200;
	
	int seconds;
	seconds = noon - sumofsec;

	return seconds;

}


For "main":
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
int main()
{
    TimeDiff timediff;

    int entHour{};  // <--- Always initialize your variables.
    int entMinute{};
    int entSec{};
    int sumofsec{};  // <--- Uninitialized this one was a problem.

    cout << "--------------Welcome to Seconds CLOCK-------------\n";

    cout << "Enter Hour: ";
    cin >> entHour;

    cout << "Enter Minute: ";
    cin >> entMinute;

    cout << "Enter Second: ";
    cin >> entSec;

    timediff.getentHour();  // <--- Need "set" functions here not "get". Or define the object of "timediff" using an overloaded ctor.
    timediff.getentMinute();
    timediff.getentSec();

    timediff.validateTime(entHour, entMinute, entSec, sumofsec);      // <--- Should be using the class variables.
    timediff.secondsSinceNoon(entHour, entMinute, entSec, sumofsec);  // <--- Should be using the class variables.

    cout << "Sum of seconds = " << timediff.getsumSec() << "\n";
    cout << "Seconds since previous noon= " << timediff.secondsSinceNoon(entHour, entMinute, entSec, sumofsec) << "\n";

    return 0;  // <--- Not required, but makes a good break point.
}


Andy
Hello !
Thank you for the recommendations. I don’t have my computer with me to make any changes at the moment but I do have some questions.

Would I remove the get functions and relabel them as set functions and use them to covert the individual values to seconds like hours to seconds and minutes to seconds ?

For the bool function I wasn’t too clear on the material on how to set this up. I looked through the book and the slide show and wasn’t sure how to get that set up to compare the values to the time values to verify if it’s valid or not /:
Hello mrsduhh,


Would I remove the get functions and relabel them as set functions and use them to covert the individual values to seconds like hours to seconds and minutes to seconds ?


Since the "get" functions are already written I would not get rid of them just yet. They may still be useful. I would write "set" functions for each variable of the class and an overloaded ctor like:
 
TimeDiff(int hour, int minutes, int seconds) : entHour(hour), entMinute(minutes), entSec(seconds), sumofsec(0) {}

Also do not forget to write a default ctor as the compiler will not provide one with an overloaded ctor. The default dtor is still provided by the compiler.


For the bool function I wasn’t too clear on the material on how to set this up.


I would refer to what jonnin said. At the moment I used 1 if statement and if any of the 3 parts is true it would return false otherwise the function would return true.

I still need to go over the instructions to make sure I understand them correctly and do some more checking.

It is late for me and I will have to work on this more in the morning.

Andy
If it helps this is the prompt I am following. It is asking I use the class TimeDiff. I am also going off by what we have done in past assignments.


: Write a class TimeDiff that contains a method called SecondsSinceNoon that takes the time as three integers, hour, minute and second. The method will return the number of seconds that have elapsed since the last time that the clock struck noon (e.g. 12, 0, 0). Time entered will be in Universal format (i.e. hours are 0-23; no AM or PM). Keep in mind that the last time the clock struck noon may be the previous day! If the time is invalid, then this method should return -1. Otherwise, it should return the number of seconds since the previous noon. If the time is invalid, the program will print an error message, and exit.
You will also create another helper method in this class called ValidateTime that validates the hour, minute and second. This method will be called by SecondsSinceNoon. This will be a boolean method that returns true if the time is valid, and false otherwise.
Upload your program, and a screenshot of the program output. You should show output for valid and invalid times. Your program should contains 3 files; TimeDiff.h, TimeDiff.cpp, and Main.cpp.
Something like this, perhaps:

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
#include <iostream>

// Write a class TimeDiff
struct time_diff // ideally should be a namespace instead of a class with all static members
{
    // helper method in this class called ValidateTime
    static bool validate_time( int hour, int minute, int second ) noexcept
    {
        // Time entered will be in Universal format (i.e. hours are 0-23) note that 24:0:0 is invalid
        return hour >= 0 && hour <= 24 && minute >= 0 && minute <= 60 && second >= 0 && second <= 60 ;
    }

    // that contains a method called SecondsSinceNoon that takes the time as three integers
    static int seconds_since_noon( int hour, int minute, int second ) noexcept
    {
        if( validate_time( hour, minute, second ) )
        {
            // compute seconds since noon on the current day
            int secs = (hour-12) * 60 * 60 + minute * 60 + second ;

            // if secs is negative, this time is before noon  on the current day,
            // the last time the clock struck noon was on the previous day
            if( secs < 0 ) secs += 24 * 60 * 60 ; // adjust secs accordingly

            return secs ;
        }

        else return -1 ; // If the time is invalid, then this method should return -1.
    }
};

int main()
{
    int hour = 0 ;
    int minute = 0 ;
    int second = 0 ;

    std::cout << "enter hour minute second separated by spaces: " ;
    if( std::cin >> hour >> minute >> second )
    {
        const int secs_since_noon = time_diff::seconds_since_noon(hour,minute,second) ;

        if( secs_since_noon >= 0 ) std::cout << "seconds since noon: " << secs_since_noon << '\n' ;
        else std::cout << "you did not enter a valid time\n" ;
    }

    else std::cout << "error: there were invalid characters in input\n" ;
}
Awesome thank you everyone here's what I have. Looks good to me but I am sure it could be better but values are outputting correctly !

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
51
#include <iostream>
#include "TimeDiff.h"

using std::cout;
using std::cin;
using std::endl;

void TimeDiff::setentHour(int hour)
{
	entHour=hour;
}
void TimeDiff::setentMinute(int minutes)
{
	entMinute=minutes;
}
void TimeDiff::setentSec(int seconds)
{
	entSec=seconds;
}
int TimeDiff::getsumSec()
{
	//total in seconds of input
	sumofsec= (entSec + (entMinute * 60) + (entHour * 60 * 60));
	return sumofsec;
}
bool TimeDiff::validateTime()
{
	return entHour >= 0 && entHour <= 24 && entMinute >= 0 && entMinute <= 60 && entSec >= 0 && entSec <= 60;
}
int TimeDiff::secondsSinceNoon(int entHour,int entMinute,int entSec)
{
	//bool validateTime();
	if (validateTime())
	{

		int secs = (((entHour - 12) * 60 * 60) + (entMinute * 60) + entSec);

		if (secs < 0)
		{
			secs += 24 * 60 * 60;
			return secs;
		}
	}
	else
	{
			cout << "Invalid Time!! \n";
			return -1;
	}
		
	
}


TimeDiff.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#ifndef TIMEDIFF_H
#define TIMEDIFF_H


class TimeDiff
{
public:
	void setentHour(int);
	void setentMinute(int);
	void setentSec(int);
	int getsumSec();
	bool validateTime();
	int secondsSinceNoon(int, int, int);

private:
	int entHour;
	int entMinute;
	int entSec;
	int sumofsec;
};


#endif 


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
#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.setentHour(entHour);
	timediff.setentMinute(entMinute);
	timediff.setentSec(entSec);
	timediff.getsumSec();

	cout << "Seconds since previous noon= " << timediff.secondsSinceNoon(entHour, entMinute, entSec) << "\n";

		return 0;
}



--------------Welcome to Seconds CLOCK-------------
Enter Hour: 9
Enter Minute: 0
Enter Second: 0
Seconds since previous noon= 75600
Your
1
2
3
4
bool TimeDiff::validateTime()
{
	return entHour >= 0 && entHour <= 24 && entMinute >= 0 && entMinute <= 60 && entSec >= 0 && entSec <= 60;
}

is broken; for instance, it would report that 10:60:43 is a valid time.

Also, if they are to be non-static members, make them const-correct.
https://isocpp.org/wiki/faq/const-correctness#const-member-fns
Topic archived. No new replies allowed.