Earliest and Latest of "All" the Logins Time

Hello People...I need help bad...this problem is wearing me out...I was able to get the program to print out the first and last logins times and then get the difference (example 1)...but when I tried to add also the earliest and latest of all the logins and difference (example 2)...all I get is nada...who can help me with this delima please...(I'm trying to print out the first time user entered and the last time user entered also the time difference between the two AND also print out the earliest and the latest of the times logins and their difference)...

Example 1: This program works.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include "time.h"
int main()
{
    int h, m, s;
    int num = 10;
    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);
    }
    cout<<"First Login is :";
    prt[0].print();
    cout<<"\nLast Login is :";
    prt[9].print();
    cout<<endl;
    while(prt[9].Equal(prt[0]) !=  true)
    {
        prt[0].incr();
        diff.incr();
    }
    cout<<"The Difference is: ";
    diff.print();
    return 0;
}


Example 2: This program don't work.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#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(0,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].GreaterThan(llogin) == true)
{
llogin = prt[x];
}
}
cout<<"\nCalculating Earliest and Latest time";
cout<<"\nThe Earliest Login Is: ";
flogin.print();
cout<<"\nThe Latest Login Is: ";
llogin.print();
return 0;
}
Last edited on
It seems that the two objectives are the same however:
Consider making the login time a decimal hour. i.e. double h e.g.(11.154)=11:9:14.
and push_back each one into a vector<double>login. Compare / print login.begin() with login.vector.end() and subtract one from the other.
Now sort the vector and repeat the compare/print operation.
Write and call a function to convert each decimal part of h to minutes and seconds...rem*60.
Hey thanks for the reply...but the objectives are quite indeed different. One ask for the first & last login & the second ask for the earliest and latest of "All" the logins combined. And I haven't used or learnt about vectors yet, so I guess I'm kind of a beginner when it comes to this dynamic array. Is there possible another solution to this problem??????
Suppose you login 20 times in one day. Each succeeding login will be later than the previous one .
So the first login will be the earliest and the last login will be the latest. If this process goes on over a period > 24 hours then you have to take the date into consideration, But it does not change the principle of 'earliest' and 'latest'. latest - earliest = negative number. If you are talking about logins from different time zones then you are quite correct.
If you are not familiar with vectors then you could use an array. You print an array by using a for loop, using the loop 'i' as the array element designator. double login [i];
The objective here is for the user to only login 9 times...from these logins...example(2pm,4pm,1pm,6pm etc)it does not have to be in order...the objective here is to find which one is the earliest and which one is the latest...the first login does not have to be the earliest and the last login does not have to be the latest...this is where my problem starts...I can get the first and last login correctly but I can not get the earliest and latest with difference done correctly...this is why I'm asking for help...
For the first/last, just loop through them all and check if each one is smaller than the previous "first".
OK.. as firedraco says look at each array element (login time) calling array[0] "small". If array[1] < "small" call it small and so on. You will be left with array [?] = small. Do it again calling array [0] "big" if array[?] > big call it "big". Now you have the earliest and latest times. You could find ealiest and latest with one run through the array by combining the two procedures above.
two possible problems.

one. you may be destroying a time value in line 18. making prt[0] equal to prt[4] may cause a problem when you need the prt[0] time in line 28 and 32.

maybe you could store prt[0] before the diff loop in lines 16-20, then restore it afterward.
1
2
3
4
5
6
7
8
Time temp;
temp = prt[0]; // is this allowed?
while(prt[4].Equal(prt[0]) !=  true)
{
prt[0].incr();
diff.incr();
}
prt[0] = temp; // is this allowed? 

two. your earliest time may never be less than 0,0,0, so you always end up with a flogin of 0,0,0. start flogin with any of the input times.

change line 24 to something like:
 
flogin = prt[1];

it should sort itself out.

maybe three. does your diff loop work when prt[4] is earlier than prt[0]? i don't really understand how you calculate diff.

maybe four. i don't see any copy/assigning except for .set() in example1. are lines 30 and 34 allowed or do you have to copy over the time by the h,m,s?
Topic archived. No new replies allowed.