#include <iostream>
using namespace std;

struct Mytime
{
   int hours;
   int mins;
};

int timecmp( Mytime t1, Mytime t2 )     // compares two Mytime values
{
   if (t1.hours == t2.hours)
   {
       if (t1.mins == t2.mins)
       {
         return 0;                      // returns 0 if times are same
       }
       else                             // mins not same
       {
          if (t1 mins > t2 mins)
           {
              return 1;        // returns >0 if first is later than second
           }
           else
           {
              return -1;       // returns <0 if first is before second
           }
        }
    }
    else                                // hours not same
    {
       if (t1.hours > t2.hours)
        {
          return 1;
        }
        else
        {
          return -1;
        }
    }
}

int setTime(Mytime& t, int h, int m );   
{
   
// if valid time sets hh and mm to h and m 
// and returns 0
// if invalid returns integer > 0 as error code
// error code +1 = underflow hours
// error code +2 = overflow hours
// error code +4 = underflow mins
// error code +8 = overflow mins
                                     
    t.hours = h;                         
    t.mins = m;
    return 0;
}

void display1Time(Mytime t);
{
     cout << "00:00";
}

void elapsed(Mytime t1, Mytime t2, Mytime& duration);
{
    setTime(duration,0,0);
}

int main()
{
    Mytime now;
    Mytime then;
    Mytime howlong;
    
    int h,m;
    
    do
    {
        cout << "Enter start hh mm : ";
        cin >> h >> m ;
    } while (setTime(now,h,m));
    
    do
    {
        cout << "Enter finish hh mm : ";
        cin >> h >> m ;
    } while (setTime(then,h,m));
    
    elapsed(now,then,howlong);

    cout << "Time elapsed from ";
    display1Time(now);
    cout << " until ";
    display1Time(then);
    
    cout << " is ";
    display1Time(howlong);
    cout << endl;
    return ( 0 );
}

Dear All
Following functions are incomplete:
int setTime............
void displayTime........
void elapsetTime.......
I would be very happy if you could complete them with comments
I am confused again
regards

As we stated in your previous thread, we will help you if you put in some effort, but we don't solve your problems for you.

When posting c++ code, please use c++ code tags
[code=c++] // Your code here

[/code]

Just pick one function at a time and write them. Lets start with set time...it has some great comments about how it should be implemented, here I'll even implement one case for you, see if you can write the other three and post your code:

int setTime(Mytime& t, int h, int m );   
{
   
// if valid time sets hh and mm to h and m 
// and returns 0
// if invalid returns integer > 0 as error code
// error code +1 = underflow hours
// error code +2 = overflow hours
// error code +4 = underflow mins
    if (m < 0) return 4;
// error code +8 = overflow mins
                                     
    t.hours = h;                         
    t.mins = m;
    return 0;
}
int setTime(Mytime& t, int h, int m );   
{
  if (h < 0) 
   {
     return 1;          // error code +1 = underflow hours
   }
   else
   {
     return 2;        // error code +2 = overflow hours
   }
   
  if (m < 0) 
   {
     return 4;          // error code +4 = underflow mins
   }
   else
   {
     return 8;        // error code +8 = overflow mins
   }
                                
    t.hours = h;                         
    t.mins = m;
    return 0;
}

here is my attempt :(((

one more thing
why we put the declaration underneath the functions.Should not they be above the functions.

It doesn't really matter which side of the comment is on, comments are for us to read, the compiler doesn't care.

Your code

if (h < 0)
{
return 1; // error code +1 = underflow hours
}
else
{
return 2; // error code +2 = overflow hours
}

Will always return 1 or 2 and never get to the rest of the code.

You aren't really testing for overflow.

(The minutes testing has the same problem.)

so what should I do now ?

if (h < 0)
{
   return 1; // error code +1 = underflow hours
}
else if( h > 59 )
{
   return 2; // error code +2 = overflow hours
}

:)

void display1Time(Mytime&t, int h, int m)
{
     cout << h << ":" << m << endl;     // Displays in form hh:mm
}

void elapsed(Mytime t1, Mytime t2, Mytime& duration)
{
    setTime(duration,0,0);
if ((duration > t1) && (duration <t2))  // determines the duration between t1 and t2
  {
    return 0;                           // t1 assumed to be earlier than t2 
  }
}

the codes above is other two functions;
"void display time" and "void elapsed"
Something missing in these two function.
I have also checked the main function, I could not find anything.

Please I need your help again.
regards

By the way:
Dear Sir Freaky Chris;
Hours should not be greater than 23
minutes should not be greater than 59
I think you put incorrect value in underflow and over flow hours on previous function.
Please warn me when I am wrong.Because I am very new student at C++
Best regards

You were right about his number, but that's not really C++, that's just basic logic. You could understand that and correct it. (And just to back you up, if you had posted that code, I would have said something, his code was a suggestion. If you want to post functions that you have completed that you're not sure are correct, I'll review them for you.)

in displayTime() you need to do something so that 5 hours and 5 minutes displays as 05:05 and not 5:5

Maybe you should print the tens digit and the ones digit of each number. If you don't understand that, you could look into "iomanip, setfill and setw".

In elapsed(), realize that t1 is the 'start' time and t2 is the 'end' time, and think back to your basic math:

  • How much time has elapsed between 08:45 and 10:15?
  • How did you figure out that it was 01:30?
  • Make the computer do that...

lol Year sorry, I wasn't really thinking, just stuck the numbers in without thinking :P

Dear Murtan
the latest version of my work as below.
I compiled it.There is no error.
But still something missin as I said before.
I would be very happy if you could give some hints
Regards

#include <iostream>
using namespace std;

struct Mytime
{
   int hours;
   int mins;
};

int timecmp( Mytime t1, Mytime t2 )     // compares two Mytime values
{
   if (t1.hours == t2.hours)
   {
       if (t1.mins == t2.mins)
       {
         return 0;                      // returns 0 if times are same
       }
       else                             // mins not same
       {
          if (t1.mins > t2.mins)
           {
              return 1;                 // returns >0 if first is later than second
           }
           else
           {
              return -1;                // returns <0 if first is before second
           }
        }
    }
    else                                // hours not same
    {
       if (t1.hours > t2.hours)
        {
          return 1;
        }
        else
        {
          return -1;
        }
    }
}

int setTime(Mytime& t, int h, int m )   
{
  if (h < 0) 
   {
       return 1;         // error code +1 = underflow hours
   }
  else if (h > 23)
   {
       return 2;         // error code +2 = overflow hours
   }
   
  if (m < 0) 
   {
     return 4;           // error code +4 = underflow mins
   }
   else if (m > 60)
   {
     return 8;           // error code +8 = overflow mins
   }
                                
    t.hours = h;                         
    t.mins = m;
    return 0;
}

void display1Time(Mytime&t, int h, int m)
{
     cout << h << ":" << m << endl;     // Displays in form hh:mm
}

void elapsed(Mytime t1, Mytime t2, Mytime& duration)
{
    setTime(duration,0,0);
if ((duration > t1) && (duration <t2))  // determines the duration between t1 and t2
  {
    return 0;                           // t1 assumed to be earlier than t2 
  }
}

int main()
{
    Mytime now;
    Mytime then;
    Mytime howlong;
    
    int h,m;
    
    do
    {
        cout << "Enter start hh mm : ";
        cin >> h >> m ;
    } while (setTime(now,h,m));
    
    do
    {
        cout << "Enter finish hh mm : ";
        cin >> h >> m ;
    } while (setTime(then,h,m));
    
    elapsed(now,then,howlong);

    cout << "Time elapsed from ";
    display1Time(now);
    cout << " until ";
    display1Time(then);
    
    cout << " is ";
    display1Time(howlong);
    cout << endl;
    return ( 0 );
}

Line 58: else if (m > 60) should compare m > 59

You have made no additional effort on display1Time() or elapsed() and I have already given you suggestions for those:

in displayTime() you need to do something so that 5 hours and 5 minutes displays as 05:05 and not 5:5

Maybe you should print the tens digit and the ones digit of each number. If you don't understand that, you could look into "iomanip, setfill and setw".

and

In elapsed(), realize that t1 is the 'start' time and t2 is the 'end' time, and think back to your basic math:

  • How much time has elapsed between 08:45 and 10:15?
  • How did you figure out that it was 01:30?
  • Make the computer do that...
#include <cmath>     // additinal library

void display1Time(Mytime&t, int h, int m)
{
  float hours = int h;
  h -= hours;
  float m = hours * 60;
  float min = int m;
  cout << h << ":" << m << endl;          // Displays in form hh:mm
}

Dear Murtan and Freaky Chris
How about this one?

No.

h ends up zero and m ends up being whatever h was * 60

Now that I think about it, why does display1Time even need h and m, aren't the hours and minutes in Mytime?

The tens and ones hint was to do something like this:

int tenmin = t.mins / 10;
int onemin = t.mins % 10;

When the time is less than ten minutes after the hour tenmin will contain zero, but when you print both numbers together you get the type of display you want. If the time is ten or more minutes after the hour, you wouldn't HAVE to split the number like that, but it will still work if you print both numbers together.

void display1Time(int h, int m)
{
  if (int m < 10)                      
  {
   cout << "0";
  }
  else  
  {                                
  cout << "00";
  }
}

{
  if ( int h < 10)
  {
    cout << "0";
  }
  else
  {
    cout << "00";
  }
}

this is my second attempt.
:(

ok you could make that work, but you forgot to print the m and h...and you should probably print the hours first.

if (h > 10) {
    cout << h;
} else {
    cout << '0' << h;
}

Remember to put the punctuation in as well.

void display1Time(int h, int m)
{
  if ( int h > 10)             //Line 77 See the error below
  {
    cout << h << endl;  //Line 80 See the error below
  }
  else
  {
    cout << "0" << h << endl;
  }
}

Errors
mytime.cc: In function `void display1Time(int, int)':
mytime.cc:77: parse error before `>'
mytime.cc:80: confused by earlier errors, bailing out

Dear Sir I dont know Why it happens?

Why are you trying to re-declare h inside the if statement?

if ( int h > 10)

should be:

if (h > 10)

Which is what I typed in my example...are you trying to break your code on purpose?

Where's the code to print the ':'?

Where's the code to support minutes?

Dear Murtan
Yes I have noticed that I accidently declared again.
Now we need to solve the problems in Void Elapsed Function.
Slowly slowly I am learning some important points but it is obvious that the experience is the most important thing.
Thank you very much for your time and patience.
I am very greatful.

Here is the Void Elapsed Function

void elapsed(Mytime t1, Mytime t2, Mytime& duration)
{
    setTime(duration,0,0);
if ((duration > t1) && (duration <t2))  // determines the duration between t1 and t2
  {
    return 0;                           // t1 assumed to be earlier than t2 
  }
}

int main()
{
    Mytime now;
    Mytime then;
    Mytime howlong;
    
    int h,m;
    
    do
    {
        cout << "Enter start hh mm : ";
        cin >> h >> m ;
    } while (setTime(now,h,m));
    
    do
    {
        cout << "Enter finish hh mm : ";
        cin >> h >> m ;
    } while (setTime(then,h,m));
    
    elapsed(now,then,howlong);

    cout << "Time elapsed from ";
    display1Time(now);
    cout << " until ";
    display1Time(then);
    
    cout << " is ";
    display1Time(howlong);
    cout << endl;
    return ( 0 );
}

Quoting ... again:

In elapsed(), realize that t1 is the 'start' time and t2 is the 'end' time, and think back to your basic math:

  • How much time has elapsed between 08:45 and 10:15?
  • How did you figure out that it was 01:30?
  • Make the computer do that...

You appear to have the same code in elapsed. You initialize duration to zero, compare it to t1 and t2 without doing anything with either value and return.

Perform the calculation suggested in the quote above using the hours and minutes from t1 and t2 and THEN initialize duration from the calculated value.

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.