im trying to compair the times in the prt array. the llogin works fine however the flogin does not. what is causing the problem given the following time.cpp thanx in adv.:
#include "time.h"
void Time::set(int h, int m, int s)
{
hour = h;
mins = m;
secs = s;
}
void Time::print ()
{
if (hour <10) cout<<"0";
cout<<hour<<":";
if (mins <10) cout<<"0";
cout<<mins<<":";
if (secs <10) cout<<"0";
cout<<secs<<endl;
}
void Time::incr()
{
secs++;
if (secs == 60)
{
mins++;
secs = 0;
}
if (mins == 60)
{
hour++;
mins = 0;
}
if (hour == 24)
{
hour = 0;
}
}
bool Time::Equal(Time t)
{
if ( hour == t.hour && mins == t.mins && secs == t.secs)
return true;
else
return false;
}
bool Time::LessThan(Time t)
{
if ( hour < t.hour || mins < t.mins || secs < t.secs)
return true;
else
return false;
}
bool Time::GraterThan(Time t)
{
if ( hour > t.hour || mins > t.mins || secs > t.secs)
return true;
else
return false;
}
=========main.cpp========
#include "time.h"
int main()
{
int h, m, s;
int num = 5;
Time *prt;
Time diff;
diff.set(0,0,0);
prt = new Time[num];
for(int x=0;x<num;x++)
{
cout<< "Enter Login#"<<x+1<<" Hour, Min, and sec\n";
cin>>h>>m>>s;
prt[x].set(h,m,s);
}
while(prt[4].Equal(prt[0]) != true)
{
prt[0].incr();
diff.incr();
}
cout<<"The Difference is: ";
diff.print();
Time flogin, llogin;
flogin.set(23,0,0);
llogin.set(0,0,0);
for(int x=0;x<num;x++)
{
if(prt[x].LessThan(flogin) == true)
{
flogin = prt[x];
}
if(prt[x].GraterThan(llogin) == true)
{
llogin = prt[x];
}
}
cout<<"\nCalculating Eirlest n LAstest time";
cout<<"\nThe Ealiest Login Is: ";
flogin.print();
cout<<"\nThe Last Login Is: ";
llogin.print();
return 0;
}
Your LessThan and [sic] GraterThan functions are both wrong.
Think about it.
1 2 3 4 5 6
bool Time::LessThan(Time t) {
if ( hour < t.hour || mins < t.mins || secs < t.secs)
returntrue;
elsereturnfalse;
}
If "*this" is 2:47:36 and "t" is 3:00:00, what will your above function
return? Clearly it should return true.
You have a similar bug in GraterThan.
BTW, a better way to write GraterThan is to check if *this is not less
than and not equal to t, because you've already written the "less than"
and "equal to" comparisons.