468,457 Members | 1,598 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,457 developers. It's quick & easy.

Creating a time class - Homework

I wrote the following time class for the following assignment.i need help in completing this program pleasee.
Write a class to hold time. Time is the hour, minute and seconds. Write a constructor that will allow the user of your class to initialize a time or set the time to all zeros if not initialized. Include all the operators listed above.

Your program will need to read from a file of times and commands. There will be a number at the top of the file that represents the number of time values in the file. Although this is the actual number of times in the file, you must read the data as if it is the maximum number of times in the file. Use a dynamic array of time objects, the size of the array will be the number listed first in the file. Your program must work for any file, we will test it with a different file (different values but the same format). Once you have read all the times from the file, continue to read the commands from the file. They are in the format of index1 operator index2. The operator will be either + or -. So if the line reads 3+2, then write to the screen the following:

time[3] + time[2] = newTime.
where time[3] is the time in position 3, and time 2 is the time in position 2, and newTime is the result of the addition. If the resultant time is > 24, then print a message first that the time is > 24 hours. Times must always be output in the correct format (hh:mm:ss) and the minutes and seconds must have valid values (<60). If the resultant time is negative, then output that message first.

When you have finished the additions and subtractions, print the times (sorted in ascending order and in the correct format) to the output file specified by the user.

Name your files time.h, time.cpp and program3.cpp.

and here the timeclass
Expand|Select|Wrap|Line Numbers
  1.  #ifndef time_H 
  2. #define time_H
  3.  
  4. #include <string>
  5. #include <iostream>
  6. #include <iomanip>
  7. using namespace std;
  8.  
  9. class timeClass
  10. {
  11. public:
  12.     //timeClass(int hours, int minuts, int seconds, float time)
  13. //    timeClass(int h=0, int m=0, int s=0) { set(h, m, s); }
  14.     void getTime(int&, int&, int&);
  15.     void setTime(int , int , int );
  16.     void printTime() const;
  17.     //void incrementHours();
  18.     //void incrementMinuts();
  19.     //void incrementSeconds();
  20.     //bool equalTime(const timeClass&) const;
  21.     timeClass(int, int, int);// constructor with parameters
  22.     timeClass();//defult constructor
  23.  
  24.     friend ostream& operator<<(ostream &out, const timeClass &Clock);
  25. friend istream& operator>>(istream &in, timeClass &Clock);
  26.     //friend timeClass operator+(const timeClass &left, const timeClass &right);
  27.     //friend timeClass operator-(const timeClass &left, const timeClass &right);
  28.     void operator+=(timeClass &t);
  29. void operator-=(timeClass &t);
  30.  
  31.  
  32.  
  33.  
  34. private:
  35.     int hrs;
  36.     int min;
  37.     int sec;
  38. };
  39. #endif
  40.  
  41. #include "time.h"
  42.  
  43. timeClass::timeClass(int hours, int minuts, int seconds)
  44. {
  45.     if(0 <= hours && hours < 24)
  46.         hrs= hours;
  47.     else
  48.         hrs = 0;
  49.     if(0 <= minuts && minuts <60)
  50.         min = minuts;
  51.     else
  52.         min = 0;
  53.     if(0 <= seconds && seconds <60)
  54.         sec=seconds;
  55.     else 
  56.         sec=0;
  57. }
  58. void timeClass::setTime(int , int , int )
  59. {
  60.     hrs=0;
  61.     min=0;
  62.     sec=0;
  63. }
  64. void timeClass::printTime() const
  65. {
  66.     if(hrs < 10)
  67.      cout<<"0";
  68.     cout << hrs << ":";
  69.  
  70.     if(min < 10)
  71.      cout << "0";
  72.     cout<< min <<":";
  73.  
  74.     if(sec < 10)
  75.      cout<<"0";
  76.     cout<< sec;
  77. }
  78.  
  79. void timeClass::getTime(int& hours, int& minutes, 
  80. int& seconds)
  81. {
  82.     hours = hrs;
  83.     minutes = min;
  84.     seconds = sec;
  85. }
  86. ostream& operator<<(ostream &out, const timeClass &Clock)
  87. {
  88.     out << Clock.hrs << " " << ":" << Clock.min << ":" << Clock.sec << endl;
  89.  
  90. return out;
  91. }
  92.  
  93. istream& operator>>(istream &in, timeClass &Clock)
  94. {
  95. in >> Clock.hrs >> Clock.min >> Clock.sec;
  96. return in;
  97. }
  98. void timeClass::operator +=(timeClass &t)
  99. {
  100. // Use the set function to take care of the addition
  101. setTime(hrs + t.hrs, min + t.min, sec + t.sec);
  102. }
  103.  
  104. void timeClass::operator -=(timeClass &t)
  105. {
  106. setTime(hrs - t.hrs, min - t.min, sec - t.sec);
  107. }
  108.  
  109. #include "time.h"
  110. #include <fstream>
  111. using namespace std;
  112.  
  113. int main()
  114. {
  115.     string inputFile, oFileName; 
  116.     ifstream fin; 
  117.     ofstream fout; 
  118.     cout<< "which file to use for input? "; 
  119.     cin>> inputFile;
  120.     fin.open(inputFile.c_str ());
  121.     if (!fin) 
  122.     { 
  123.         cerr<< "Unable to open input file \n"; 
  124.         exit(2); 
  125.     } 
  126.     cout << "which file to use for output? "; 
  127.     cin >> oFileName; 
  128.     fout.open(oFileName.c_str ()); 
  129.     if (!fout) 
  130.     { 
  131.         cerr<< "Unable to open output file \n"; 
  132.         exit(3); 
  133.     } 
  134.  
  135.     int t1, t2, result;
  136.  
  137.      result=t1+t2;
  138.      cout << "result >24"<<endl;
  139. //close all files
  140.     fin.close();
  141.     fout.close();
  142.     return 0;
  143.  
  144. }
  145.  
Oct 19 '06 #1
7 7080
vermarajeev
180 100+
Can you please post the contents of the input file to be read by your program??...

Thankx
Oct 19 '06 #2
What specific problems are you having? Is it with opening your input file?

If it's no liking inFile.c_str try this code:
Expand|Select|Wrap|Line Numbers
  1. char * temp;
  2. strcpy(temp, inFile.c_str());
  3. fin.open(temp);
Oct 19 '06 #3
here's the input

356
2:23:58
16:20:56
18:30:20
21:29:53
4:20:15
3:13:43
20:41:51
21:1:16
3:23:35
4:49:10
20:59:47
16:41:59
18:56:38
22:7:49
23:8:57
21:16:55
14:28:18
15:29:12
0:43:47
13:53:54
4:50:12
12:59:58
18:20:44
21:53:55
0:46:31
7:18:54
12:19:40
4:7:12
14:24:42
22:28:35
14:39:58
5:5:42
13:32:15
21:27:1
4:11:51
6:18:31
0:24:33
22:51:29
17:52:17
10:24:53
7:46:10
7:56:52
15:44:14
3:26:23
14:23:26
20:57:23
4:30:7
3:36:43
20:23:35
14:18:22
15:49:27
20:11:18
0:49:41
11:51:11
2:21:21
5:44:52
23:45:0
23:54:44
19:32:5
0:29:9
17:5:20
11:30:27
22:14:5
9:13:45
13:30:1
20:5:25
14:57:39
9:10:39
1:56:52
14:35:14
20:42:38
3:32:20
7:20:16
2:27:44
22:35:30
13:1:35
5:52:36
5:22:26
13:36:48
11:22:3
4:14:40
19:34:54
16:8:58
8:12:33
4:44:31
3:57:57
5:34:31
21:9:18
22:4:4
3:38:39
2:32:26
7:31:40
10:40:49
23:55:27
14:7:32
19:41:32
18:13:40
21:0:4
6:35:2
4:26:2
23:20:34
12:28:57
5:20:36
0:56:5
3:36:29
5:22:29
19:17:31
7:10:37
6:46:8
1:5:13
18:44:26
5:59:47
21:30:14
21:7:38
13:19:4
1:36:7
9:43:20
11:43:38
10:34:31
14:32:31
12:49:4
19:6:11
2:46:12
9:24:7
2:5:47
17:39:0
7:28:50
12:58:47
5:48:16
15:34:51
13:45:48
9:2:44
17:28:13
22:22:34
4:57:47
18:26:4
10:56:21
0:17:27
21:13:8
8:32:15
18:7:31
16:32:45
11:22:58
10:22:47
16:35:2
14:23:9
21:34:44
8:29:7
22:30:42
5:34:50
17:18:44
15:22:37
18:33:6
7:34:21
11:10:22
23:6:34
16:31:11
2:51:20
7:12:43
11:28:37
5:49:5
4:33:29
3:47:33
0:13:29
7:4:35
20:21:29
1:29:8
8:34:20
20:42:0
10:12:10
19:47:38
0:48:23
1:1:43
18:18:49
17:17:3
9:44:54
20:33:51
12:21:32
7:58:46
20:33:46
20:48:16
15:31:23
10:9:47
8:23:30
23:59:32
6:38:12
5:44:38
0:41:19
5:55:50
3:41:25
15:44:32
0:42:24
4:50:27
10:35:30
16:10:16
23:51:41
3:43:58
0:48:55
20:40:55
19:33:50
3:35:58
21:7:23
7:48:56
8:13:7
10:13:46
15:11:13
2:21:50
10:20:22
10:23:32
16:16:26
13:14:15
21:56:56
10:7:25
18:56:43
6:43:0
10:58:52
12:33:45
1:34:17
9:13:1
12:14:17
8:57:46
2:11:19
16:54:5
19:1:34
15:16:54
18:51:11
5:37:21
0:50:39
2:38:25
16:49:19
13:51:23
9:59:6
4:35:15
23:10:25
8:35:31
15:16:4
6:19:30
14:55:26
15:54:2
22:28:45
18:47:3
10:42:19
6:30:27
4:43:25
2:45:15
22:43:22
11:48:18
20:56:20
1:17:4
16:14:27
13:0:25
20:9:55
18:1:57
23:55:20
8:25:9
21:16:28
16:2:38
12:33:42
8:24:9
10:47:33
23:17:28
6:48:34
13:41:9
5:7:32
2:14:55
9:21:33
6:20:23
21:8:13
4:31:57
8:18:54
17:1:1
12:16:8
19:11:15
18:38:16
2:3:22
23:50:41
17:47:25
11:47:14
2:57:57
14:1:57
21:20:3
19:2:38
18:53:58
15:25:46
2:23:25
4:3:14
9:53:29
13:36:10
15:26:12
7:9:54
19:48:8
22:41:25
0:28:54
0:55:31
2:11:25
4:45:23
19:59:51
11:48:37
17:55:3
3:38:24
13:16:13
21:5:31
15:17:23
12:41:19
5:16:20
7:52:47
17:15:2
1:40:7
17:1:43
8:43:33
19:37:58
9:4:13
9:30:25
10:58:30
17:25:43
21:31:18
18:18:16
23:54:9
1:59:56
19:20:20
12:58:26
13:7:44
20:44:41
19:38:59
5:7:57
15:43:34
21:15:50
0:7:49
7:8:13
12:55:51
17:29:43
0:18:27
16:24:36
21:26:25
8:10:45
14:39:13
9:57:17
11:40:31
11:13:14
23:13:56
10:14:13
6:45:39
7:37:5
12:53:46
20:23:34
21:26:40
9:11:53
7:46:31
23:40:49
10:7:19
18:26:35
16:41:37
21:24:37
20:3:34
21:8:37
1:30:41
167+57
90-176
247+171
344+143
127-211
274-107
350+197
264-275
194-277
336-178

thanks
Oct 23 '06 #4
What specific problems are you having? Is it with opening your input file?

If it's no liking inFile.c_str try this code:
Expand|Select|Wrap|Line Numbers
  1. char * temp;
  2. strcpy(temp, inFile.c_str());
  3. fin.open(temp);
my problem is that at the end of the input file there is the lines that we have to add or subtract for example 90+50 which means that add the time in line 90 to the time in line 50
thanks
Oct 23 '06 #5
vermarajeev
180 100+
I will just give a simple code to read the total entries in the file that you have given which is 356...

Here it goes
Expand|Select|Wrap|Line Numbers
  1. int main()
  2. {
  3.     string inputFile = "time.txt";  //the file named by me 
  4.     ifstream fin;     
  5.     char line[256];
  6.  
  7.     fin.open(inputFile.c_str ());
  8.     if (!fin) 
  9.     { 
  10.         cerr<< "Unable to open input file \n"; 
  11.         exit(2); 
  12.     }
  13.     fin.getline(line, sizeof(line));
  14.     istringstream is(line);
  15.     int total = 0;
  16.     is>>total;
  17.     cout<<total<<endl;
  18.     timeClass* obj = new timeClass[total];    
  19.     //Give a try from here...
  20.  
  21.     //close all files
  22.     fin.close();    
  23.     return 0;
  24. }
Oct 24 '06 #6
vermarajeev
180 100+
You can even try out in this way

Expand|Select|Wrap|Line Numbers
  1. class Time 
  2. {
  3. public:
  4.    Time();               // default constructor
  5.    void setTime(int, int, int);  
  6.    void printMilitary();         
  7.    void printStandard();         
  8. private:
  9.    int hour;     // 0 - 23
  10.    int minute;   // 0 - 59
  11.    int second;   // 0 - 59
  12. };
  13.  
  14. Time::Time() 
  15.     hour = minute = second = 0; 
  16. }
  17.  
  18. void Time::setTime(int h, int m, int s)
  19. {
  20.    hour = (h >= 0 && h < 24) ? h : 0;
  21.    minute = (m >= 0 && m < 60) ? m : 0;
  22.    second = (s >= 0 && s < 60) ? s : 0;
  23. }
  24.  
  25. // Print Time in military format
  26. void Time::printMilitary()
  27. {
  28.    cout << (hour < 10 ? "0" : "") << hour << ":"
  29.     << (minute < 10 ? "0" : "") << minute << ":"
  30.     << (second < 10 ? "0" : "") << second;
  31. }
  32.  
  33. // Print time in standard format
  34. void Time::printStandard()
  35. {
  36.    cout << ((hour == 0 || hour == 12) ? 12 : hour % 12)
  37.     << ":" << (minute < 10 ? "0" : "") << minute
  38.     << ":" << (second < 10 ? "0" : "") << second
  39.     << (hour < 12 ? " AM" : " PM");
  40. }
  41.  
  42. // Driver to test simple class Time
  43. int main()
  44. {
  45.     string inputFile = "time.txt"; 
  46.     ifstream fin;     
  47.     char line[256];
  48.  
  49.     fin.open(inputFile.c_str ());
  50.     if (!fin) 
  51.     { 
  52.     cerr<< "Unable to open input file \n"; 
  53.     exit(2); 
  54.     }
  55.     fin.getline(line, sizeof(line));
  56.     istringstream is(line);
  57.     int total = 0;
  58.     is>>total;
  59.  
  60.     Time* t = new Time[total];
  61.  
  62.     int hr = 0, min = 0, sec = 0;
  63.     char ch;
  64.     for(int i=0; i<total; ++i)
  65.     {
  66.         fin.getline(line, sizeof(line));        
  67.         istringstream is(line);
  68.         is>>hr>>ch>>min>>ch>>sec;
  69.         t[i].setTime(hr, min, sec);        
  70.     }
  71.     cout << "\n\nMilitary time after setTime is "<<endl;
  72.     for(int i=0; i<total; ++i)
  73.     {
  74.         t[i].printMilitary();
  75.         cout<<"\t";
  76.     }
  77.     cout << "\nStandard time after setTime is "<<endl;
  78.     for(int i=0; i<total; ++i)
  79.     {
  80.         t[i].printStandard();
  81.         cout<<"\t";
  82.     }
  83.             fin.close();
  84.  
  85.    return 0;
  86. }
Oct 24 '06 #7
vermarajeev
180 100+
my problem is that at the end of the input file there is the lines that we have to add or subtract for example 90+50 which means that add the time in line 90 to the time in line 50
thanks
And here is the solution for above quotes
Expand|Select|Wrap|Line Numbers
  1. int index1 = 0, index2 = 0;
  2. char op;    
  3. Time resultant;
  4. while(!fin.eof())
  5. {
  6.    fin.getline(line, sizeof(line));
  7.    istringstream is(line);
  8.    is>>index1>>op>>index2;
  9.    switch(op)
  10.    {
  11.        case '+':
  12.        resultant = t[index1] + t[index2];
  13.   resultant.setTime(resultant.getHr(), resultant.getMin(), resultant.getSec()); 
  14.        resultant.printMilitary();
  15.        cout<<endl;
  16.        resultant.printStandard();
  17.        cout<<endl;
  18.        break;
  19.        case '-':
  20.        resultant = t[index1] - t[index2];
  21.   resultant.setTime(resultant.getHr(), resultant.getMin(), resultant.getSec()); 
  22.        resultant.printMilitary();
  23.        cout<<endl;
  24.        resultant.printStandard();
  25.        cout<<endl;
  26.        break;    
  27.     }
  28.  
  29. }
Oct 24 '06 #8

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

16 posts views Thread by Merlin | last post: by
7 posts views Thread by mike | last post: by
15 posts views Thread by Carlos Lozano | last post: by
12 posts views Thread by Mats Lycken | last post: by
9 posts views Thread by Brian | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by subhajit12345 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.