954,480 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Digital Time Program

Okay fellows. I got a really tough one. I got it licked pretty much except for one part. This is the implementation of the ADT DigitalTime. You are to enter two different times in military format (i.e. 1:30 pm is 13:30. It is suppose to spit out how much time is between two designated times. That is in a test file. I have the implementation file, the interface file (dtime.h) which was given and the application file (timedemo91.cpp) which was also given. The only problem I am having is in the main program section. It is the section:

void DigitalTime::advance(int hours_added, int minutes_added)
{
hour = (hour + hours_added)%24;
advance(minutes_added);
}

Can you take a look and tell me your thoughts?

Attachments assignment6a1.cpp (4.4KB) dtime91.h (2.19KB) timedemo91.cpp (1.21KB)
konacious
Newbie Poster
20 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

you have not coded the implementation of interval_since().

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

Can you give me your thoughts on this piece of code:

void DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutes_in_interval) const
    {
      		if (time1.hour > time2.hour)
		{
			hours_in_interval = time1.hour - time2.hour;
				
			if (time1.minute > time2.minute
			{
			 minutes_in_interval = time1.minute - time2.minute
			}
			else
			}
			 minutes_in_interval = time2.minute - time1.minute
			{

		}
		else
		{
			hours_in_interval = time2.hour - time1.hour;
		}
   }


<< moderator edit: added code tags : [code][/code] >>

konacious
Newbie Poster
20 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

Make it easy for yourself -- convert both time1 and time2 into minutes, subtract them and use the absolute value. No need for all those comparisons.

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

so you mean like

hours_in_interval = abs((time1.hour*60 - time2.hour*60))
hours_in_interval = hours_in_interval%60
minutes_in_interval = abs(time1.minute - time2.minute)

konacious
Newbie Poster
20 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

almost

t1 = (time1.hour*60) * time1.minutes;
t2 = (time2.hour*60) + time2.minutes.
diff = abs(t2 - t1);
hours_in_interval  = diff % 60;
minutes_in_interval = = diff/60;
Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

int t1, t2, diff;
t1 = (time1.hour*60) * time1.minute;
t2 = (time2.hour*60) + time2.minute;
diff = abs(t2 - t1);
hours_in_interval = diff % 60;
minutes_in_interval = diff/60;

Okay, I got what you're saying. I still get the following errors:


prototype for `void dtimesavitch::DigitalTime::interval_since(const dtimesavitch::DigitalTime&, int&, int&) const' does not match any in class `dtimesavitch::DigitalTime'dtime91.h:38: error: candidate is: void dtimesavitch::DigitalTime::interval_since(const dtimesavitch::DigitalTime&, int&, int&)problem91.cpp: In member function `void dtimesavitch::DigitalTime::interval_since(const dtimesavitch::DigitalTime&, int&, int&) const':

--------------------------------------------------------------------------------
Source Line
Line Number 78
Compiler Error `time1' undeclared (first use this function)

--------------------------------------------------------------------------------
Source Line
Line Number 78
Compiler Error (Each undeclared identifier is reported only once for each function it appears in.)

--------------------------------------------------------------------------------
Source Line
Line Number 79
Compiler Error `time2' undeclared (first use this function)

<< moderator edit: disabled smilies >>

konacious
Newbie Poster
20 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

That does not supprise me. You cannot just plunk the example code I posted into your program and expect it to compile. You will have to change it to fit in your program, or modify your program to use the code.

The prototype error means that there is a difference between what you put in the class and what you write in the implementation file. Both functions must match exactly (except for virtual keyword if it exists).

Ancient Dragon
Retired & Loving It
Team Colleague
30,049 posts since Aug 2005
Reputation Points: 5,662
Solved Threads: 2,343
 

I got it. The key was:

if (diff < 0)

diff += 24 * 60;

I thank you for your help and I solved it as well as got a complete understanding.

konacious
Newbie Poster
20 posts since Aug 2005
Reputation Points: 10
Solved Threads: 0
 

u have 2 b careful when using functions and classes becoz they change the program a lot as i have just found out. If the arguments 2 each function is not as it is defined it will not work. I hop this helps! :D

iamthwee
Posting Expert
5,950 posts since Aug 2005
Reputation Points: 1,543
Solved Threads: 439
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You