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?

Edited 3 Years Ago by happygeek: fixed formatting

Attachments
//Filename:  Assignment5a1.cpp
//Author:  Gregory Speights
//Assignment Number:  6A1
//Description:  This is the IMPLEMENTATION of the ADT DigitalTime
//Last Changed:  August 16, 2005

#include <iostream>
#include <cctype>
#include <cstdlib>
#include "dtime91.h"
using namespace std;

namespace
{
    //These function declarations are for use in the definition of 
    //the overloaded input operator >>:

    void read_hour(istream& ins, int& the_hour);
    //Precondition: Next input in the stream ins is a time in notation,
    //like 9:45 or 14:45.
    //Postcondition: the_hour has been set to the hour part of the time. 
    //The colon has been discarded and the next input to be read is the minute.

    void read_minute(istream& ins, int& the_minute);
    //Reads the minute from the stream ins after read_hour has read the hour.

    int digit_to_int(char c);
    //Precondition: c is one of the digits 0 through 9.
    //Returns the integer for the digit; for example, digit_to_int(3) returns 3.
}//unnamed namespace


namespace dtimesavitch
{
    bool operator ==(const DigitalTime& time1, const DigitalTime& time2)
    {
        return (time1.hour == time2.hour && time1.minute == time2.minute);
    }

    DigitalTime::DigitalTime( ) : hour(0), minute(0)
    {
        //Body intentionally empty.
    }

    //Uses iostream and cstdlib:
    DigitalTime::DigitalTime(int the_hour, int the_minute)
    {
        if (the_hour < 0 || the_hour > 23 || the_minute < 0 || the_minute > 59)
        {
            cout << "Illegal argument to DigitalTime constructor.";
            exit(1);
        }
        else
        {
            hour = the_hour;
            minute = the_minute;
        }
    }

    void DigitalTime::advance(int minutes_added)
    {
        int gross_minutes = minute + minutes_added;
        minute = gross_minutes%60;

        int hour_adjustment = gross_minutes/60;
        hour = (hour + hour_adjustment)%24;
    }

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


	// --------------------------------
	// ----- ENTER YOUR CODE HERE -----
	// --------------------------------


	// --------------------------------
	// --------- END USER CODE --------
	// --------------------------------


    //Uses iostream:
    ostream& operator <<(ostream& outs, const DigitalTime& the_object)
    {
        outs << the_object.hour << ':';
        if (the_object.minute < 10)
            outs << '0';
        outs << the_object.minute;
        return outs;
    }

    //Uses iostream and functions in the unnamed namespace:
    istream& operator >>(istream& ins, DigitalTime& the_object)
    {
        read_hour(ins, the_object.hour);
        read_minute(ins, the_object.minute);
        return ins;
    }
} //dtimesavitch


namespace
{
    int digit_to_int(char c)
    {
        return ( int(c) - int('0') );
    }

    //Uses iostream, cctype, and cstdlib:
    void read_minute(istream& ins, int& the_minute)
    {
        char c1, c2;
        ins >> c1 >> c2;

        if (!(isdigit(c1) && isdigit(c2)))
        {
            cout << "Error illegal input to read_minute\n";
            exit(1);
        }

        the_minute = digit_to_int(c1)*10 + digit_to_int(c2);

        if (the_minute < 0 || the_minute > 59)
        {
            cout << "Error illegal input to read_minute\n";
            exit(1);
        }
    }

    //Uses iostream, cctype, and cstdlib:
    void read_hour(istream& ins, int& the_hour)
    {
        char c1, c2;
        ins >> c1 >> c2;
        if ( !( isdigit(c1) && (isdigit(c2) || c2 == ':' ) ) )
        {
            cout << "Error illegal input to read_hour\n";
            exit(1);
        }

        if (isdigit(c1) && c2 == ':')
        {
            the_hour = digit_to_int(c1);
        }
        else //(isdigit(c1) && isdigit(c2))
        {
            the_hour = digit_to_int(c1)*10 + digit_to_int(c2);
            ins >> c2;//discard ':'
            if (c2 != ':')
            {
                cout << "Error illegal input to read_hour\n";
                exit(1);
            }
        }

        if ( the_hour < 0 || the_hour > 23 )
        {
            cout << "Error illegal input to read_hour\n";
            exit(1);
        }
    }

}//unnamed namespace
//Header file dtime.h: This is the INTERFACE for the class DigitalTime.
//Values of this type are times of day. The values are input and output in
//24 hour notation as in 9:30 for 9:30 AM and 14:45 for 2:45 PM.

#ifndef DTIME_H
#define DTIME_H

#include <iostream>
using namespace std;

namespace dtimesavitch
{

    class DigitalTime
{
    public:
        friend bool operator ==(const DigitalTime& time1, const DigitalTime& time2);
       //Returns true if time1 and time2 represent the same time; 
        //otherwise, returns false.

        DigitalTime(int the_hour, int the_minute);
        //Precondition: 0 <= the_hour <= 23 and 0 <= the_minute <= 59.
        //Initializes the time value to the_hour and the_minute.

        DigitalTime( );
        //Initializes the time value to 0:00 (which is midnight).

        void advance(int minutes_added);
        //Precondition: The object has a time value.
        //Postcondition: The time has been changed to minutes_added minutes later.

        void advance(int hours_added, int minutes_added);
        //Precondition: The object has a time value.
        //Postcondition: The time value has been advanced
        //hours_added hours plus minutes_added minutes.

        void interval_since(const DigitalTime& a_previous_time,
            int& hours_in_interval, int& minutes_in_interval);
        // Precondition: The object and a_previous_time both have time values
        // Postcondition: hours_in_interval and minutes_in_interval contain
        //   the interval between the two times

        friend istream& operator >>(istream& ins, DigitalTime& the_object);
        //Overloads the >> operator for input values of type DigitalTime.
        //Precondition: If ins is a file input stream, then ins has already been 
        //connected to a file. 

        friend ostream& operator <<(ostream& outs, const DigitalTime& the_object);
        //Overloads the << operator for output values of type DigitalTime.
        //Precondition: If outs is a file output stream, then outs has already been 
        //connected to a file.
    private:
        int hour;
        int minute;
    };
}//end dtimesavitch

#endif //DTIME_H
#include <iostream>
#include <cctype>
#include <cstdlib>
#include "dtime91.h"
using namespace std;
using namespace dtimesavitch;


int main( )
{
    DigitalTime current(5, 45), previous(2, 30);
    DigitalTime current2(15, 00), previous2(14, 59);
    int hours, minutes;

    current.interval_since(previous, hours, minutes);
    cout << "The time interval between " << previous
        << " and " << current << endl
        << "is " << hours << " hours and "
        << minutes << " minutes.\n";

    previous.interval_since(current, hours, minutes);
    cout << "The time interval between " << current
        << " and " << previous << endl
        << "is " << hours << " hours and "
        << minutes << " minutes.\n";

    current2.interval_since(previous2, hours, minutes);
    cout << "The time interval between " << previous2
        << " and " << current2 << endl
        << "is " << hours << " hours and "
        << minutes << " minutes.\n";

    previous2.interval_since(current2, hours, minutes);
    cout << "The time interval between " << current2
        << " and " << previous2 << endl
        << "is " << hours << " hours and "
        << minutes << " minutes.\n";

    return 0;
}

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] >>

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.

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)

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;

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

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).

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.

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

This question has already been answered. Start a new discussion instead.