Problem with Time Program

Hi, I have a program which is supposed to give the time elapsed from a given time to another given time. There were three functions which were incomplete setTime(), display1Time()and elapsed(). I have created a solution for two of the functions, but found that there are some erros which I do not understand and was wondering if I can get your help to correct these errors and make the program work completely.My code is displayed below:

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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
// Incomplete version
// Starting code for student tutorial

#include <iostream>
using namespace std;

struct Mytime
{
   int hours;
   int mins;
};

int timecmp( Mytime, Mytime );
// compares two Mytime values
// returns 0 if times are same
// returns >0 if first is later than second
// returns <0 if first is before second

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

void display1Time( Mytime t );
// displays in form hh:mm

void elapsed( Mytime t1, Mytime t2, Mytime& duration );
// determines the time duration between t1 and t2
// t1 assumed to be earlier than t2

int main()
{
    Mytime now;
    Mytime then;
    Mytime howlong;
    
    int h,m;
    
    do // validate input
    {
        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 );
}


int timecmp(Mytime t1,Mytime t2)
{
   if (t1.hours == t2.hours)
   {
      if (t1.mins == t2.mins)
      {
         return 0;
      }
      else // mins not same
      {
         if (t1.mins > t2.mins)
         {
            return 1; // greater than zero
         }
         else
         {
            return -1;
         }
      }
   }
   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)
{
  if (h > 10) 
   {
      cout << h << endl;
   }
  else
   {
      cout << "0" << h << endl;
   }
  if (m > 10)
   {
      cout << m << endl;
   }
  else
   {
      cout << "0" << m << endl;
   }

  cout << h << ":" << m << endl;
}


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


I have completed the functions for setTime() and display1Time(), but these have caused the following errors:

1
2
3
4
5
6

time.cc: In function `void display1Time(Mytime)':
time.cc:133: `h' undeclared (first use this function)
time.cc:133: (Each undeclared identifier is reported only once
time.cc:133: for each function it appears in.)
time.cc:141: `m' undeclared (first use this function) 


I tried correcting this error by changing the function to void display1Time (int h, int m) but this did not work saying the following:

1
2
3
4
5
6

16 jaguar% g++ time.cc
time.cc: In function `int main()':
time.cc:58: `struct Mytime' used where a `int' was expected
time.cc:60: `struct Mytime' used where a `int' was expected
time.cc:63: `struct Mytime' used where a `int' was expected 


Hope you can help me. Also, I would like to know how to approach the function elapsed().

Thank you if you can help.
Can anyone help me at all?
Well, the first set of compile errors is pretty obvious. You use variables named "h" and "m" inside the function but no such variables with those names exist.

So you figured that out and changed the parameters to display1Time(). But your code calls that function with the "old" parameter. And you got rid of that and replaced it with two. Hence the second set of compile errors: the function now takes ( int, int ) but you are passing (Mytime). Mytime is not int.

So undo the change you made, first of all and go back to what you have above. display1Time needs the hours and minutes which you are calling h and m. Does not Mytime have two fields called hours and mins, which are probably what you want?

elapsed(): The time is currently 8:21. How much time has elapsed since 5:37? How did you figure out the answer?

8:21
- 5:37
-------
?

21 is less than 37, so change the 8 to a 7 and add 60 to 21:

7:81
- 5:37
------
2:44

81-37 = 44; 7-5 = 2. Done.

The only other case you need to worry about is when you don't need to do the "borrow" of 60 from the hours.

Now go implement it.
I am still having a little difficulty on the display1Time() function. Can you explain that again please?
My latest version of the display1Time() function is below:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
void display1Time( Mytime t)
{
  if (t.hours > 10)
    {
      cout << t.hours << endl;
    }
  else
    {
      cout << "0" << t.hours << endl;
    }
  if (t.mins > 10)
    {
      cout << t.mins << endl;
    }
  else
    {
      cout << "0" << t.mins << endl;
    }

  cout << t.hours << ":" << t.mins << endl;
}


But when I compile it, I seem to get the following:

1
2
3
4
5
6
7
8
9
10
11
12
13
9 jaguar% g++ time.cc
10 jaguar% ./a.out
Enter start hh mm : 7 30
Enter finish hh mm : 7 29
Time elapsed from 07
30
7:30
 until 07
29
7:29
 is 00
00
0:0


I know the elapse() function is still yet to be done, so it will of course not display the elapsed time from 7 30 to 7 29, but any help as to why when I compile it, it gives me this would be good.
Your display1Time function is printing the time twice, once in the if-else statements and once as the last line.

btw, try out this:

cout << setw( 2 ) << setfill( '0' ) << 5;

you might find it helpful.

Topic archived. No new replies allowed.