Heey guys... I'm in a rush.. I don't know how to write on definition of a function here. Please help.

The function interval_since is supposed to compute the time. The previous time and the current. I'm confused how to write the definition.

Here is my code.. and the very last part of it is incomplete and it is where I need help.
Oh also in the main I'm getting 2 erros for an nnkown reason, would u please check it out.

//Interface File for DigitalTime 
//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.

#include <iostream>
using namespace std;

class DigitalTime
{
public:


	void interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutesJ_in_interval) const;

    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.

    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;
};
//------------------------------------------------------------------------------------------------------------
int main ()
{
	DigitalTime current(5, 45), previous(2,30);
	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";



return 0;
}

//--------------------------------------------------------------------------------------------------------------

//Implementation File for DigitalTime
//This is the IMPLEMENTATION of the ADT DigitalTime.


//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 24-hour 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.

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

//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;
    }
}
DigitalTime::DigitalTime( ) : hour(0), minute(0)
{
    //Body intentionally empty.
}


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

//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:
istream& operator >>(istream& ins, DigitalTime& the_object)
{
    read_hour(ins, the_object.hour);
    read_minute(ins, the_object.minute);
    return ins;
}

int digit_to_int(char c)
{
    return ( static_cast<int>(c) - static_cast<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);
    }
}


void DigitalTime::interval_since(const DigitalTime& a_previous_time, int& hours_in_interval, int& minutesJ_in_interval) const
{
int a;

a= (hours_in_interval - minutesJ_in_interval);
return a;
}

Recommended Answers

All 4 Replies

Line 213 returns an int but line 217 says return type is void.

A protocol for calculating time interval between current time and other time might look something like:

//detrmine which time is ealier than the other
if current time after other time
   later time = current time
   earlier time = other time
else
   later time = other time
   earlier time = current time

//check if need to adjust minutes to keep interval value positive
if later time minutes less than earlier time minutes 
   add 60 to later time minutes 
   subtract 1 from later time hour;

//calculate interval in minutes and hours
minutes = later time minutes minus earlier time minutes
hours = later time hour minu earlier time hour

//minutes and hours will be returned to main by reference.

Posting the error messages you get would be helpful in determining what your are getting them. I would guess you need to put a semicolon after line 57 and start line 58 as a new cout statement given the flush of cout that occurs with endl, but I don't know that for sure.

My dear friend i'm aware of what you have just said.
But I'm not able to put all that in a proper code.
What you've just said is really helpful though, thanks.

Please I need a proper code for this !
Yo people help !!

It's hard to believe you managed the rest of the code for you to flail around so helplessly on this function, even when given a hint on how to solve it.

Your 'advance' functions already contain the essential elements of the maths to manipulate time in hours and minutes. It's the same deal, work everything out in minutes, do some calculation, then convert back to hours/minutes.

Bumping your own thread with "help" and then "help" without posting ANY further effort just puts people off.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.