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 - #ifndef time_H
-
#define time_H
-
-
#include <string>
-
#include <iostream>
-
#include <iomanip>
-
using namespace std;
-
-
class timeClass
-
{
-
public:
-
//timeClass(int hours, int minuts, int seconds, float time)
-
// timeClass(int h=0, int m=0, int s=0) { set(h, m, s); }
-
void getTime(int&, int&, int&);
-
void setTime(int , int , int );
-
void printTime() const;
-
//void incrementHours();
-
//void incrementMinuts();
-
//void incrementSeconds();
-
//bool equalTime(const timeClass&) const;
-
timeClass(int, int, int);// constructor with parameters
-
timeClass();//defult constructor
-
-
friend ostream& operator<<(ostream &out, const timeClass &Clock);
-
friend istream& operator>>(istream &in, timeClass &Clock);
-
//friend timeClass operator+(const timeClass &left, const timeClass &right);
-
//friend timeClass operator-(const timeClass &left, const timeClass &right);
-
void operator+=(timeClass &t);
-
void operator-=(timeClass &t);
-
-
-
-
-
private:
-
int hrs;
-
int min;
-
int sec;
-
};
-
#endif
-
-
#include "time.h"
-
-
timeClass::timeClass(int hours, int minuts, int seconds)
-
{
-
if(0 <= hours && hours < 24)
-
hrs= hours;
-
else
-
hrs = 0;
-
if(0 <= minuts && minuts <60)
-
min = minuts;
-
else
-
min = 0;
-
if(0 <= seconds && seconds <60)
-
sec=seconds;
-
else
-
sec=0;
-
}
-
void timeClass::setTime(int , int , int )
-
{
-
hrs=0;
-
min=0;
-
sec=0;
-
}
-
void timeClass::printTime() const
-
{
-
if(hrs < 10)
-
cout<<"0";
-
cout << hrs << ":";
-
-
if(min < 10)
-
cout << "0";
-
cout<< min <<":";
-
-
if(sec < 10)
-
cout<<"0";
-
cout<< sec;
-
}
-
-
void timeClass::getTime(int& hours, int& minutes,
-
int& seconds)
-
{
-
hours = hrs;
-
minutes = min;
-
seconds = sec;
-
}
-
ostream& operator<<(ostream &out, const timeClass &Clock)
-
{
-
out << Clock.hrs << " " << ":" << Clock.min << ":" << Clock.sec << endl;
-
-
return out;
-
}
-
-
istream& operator>>(istream &in, timeClass &Clock)
-
{
-
in >> Clock.hrs >> Clock.min >> Clock.sec;
-
return in;
-
}
-
void timeClass::operator +=(timeClass &t)
-
{
-
// Use the set function to take care of the addition
-
setTime(hrs + t.hrs, min + t.min, sec + t.sec);
-
}
-
-
void timeClass::operator -=(timeClass &t)
-
{
-
setTime(hrs - t.hrs, min - t.min, sec - t.sec);
-
}
-
-
#include "time.h"
-
#include <fstream>
-
using namespace std;
-
-
int main()
-
{
-
string inputFile, oFileName;
-
ifstream fin;
-
ofstream fout;
-
cout<< "which file to use for input? ";
-
cin>> inputFile;
-
fin.open(inputFile.c_str ());
-
if (!fin)
-
{
-
cerr<< "Unable to open input file \n";
-
exit(2);
-
}
-
cout << "which file to use for output? ";
-
cin >> oFileName;
-
fout.open(oFileName.c_str ());
-
if (!fout)
-
{
-
cerr<< "Unable to open output file \n";
-
exit(3);
-
}
-
-
int t1, t2, result;
-
-
result=t1+t2;
-
cout << "result >24"<<endl;
-
//close all files
-
fin.close();
-
fout.close();
-
return 0;
-
-
}
-
7 7663
Can you please post the contents of the input file to be read by your program??...
Thankx
What specific problems are you having? Is it with opening your input file?
If it's no liking inFile.c_str try this code: - char * temp;
-
strcpy(temp, inFile.c_str());
-
fin.open(temp);
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
What specific problems are you having? Is it with opening your input file?
If it's no liking inFile.c_str try this code: - char * temp;
-
strcpy(temp, inFile.c_str());
-
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
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 - int main()
-
{
-
string inputFile = "time.txt"; //the file named by me
-
ifstream fin;
-
char line[256];
-
-
fin.open(inputFile.c_str ());
-
if (!fin)
-
{
-
cerr<< "Unable to open input file \n";
-
exit(2);
-
}
-
fin.getline(line, sizeof(line));
-
istringstream is(line);
-
int total = 0;
-
is>>total;
-
cout<<total<<endl;
-
timeClass* obj = new timeClass[total];
-
//Give a try from here...
-
-
//close all files
-
fin.close();
-
return 0;
-
}
You can even try out in this way - class Time
-
{
-
public:
-
Time(); // default constructor
-
void setTime(int, int, int);
-
void printMilitary();
-
void printStandard();
-
private:
-
int hour; // 0 - 23
-
int minute; // 0 - 59
-
int second; // 0 - 59
-
};
-
-
Time::Time()
-
{
-
hour = minute = second = 0;
-
}
-
-
void Time::setTime(int h, int m, int s)
-
{
-
hour = (h >= 0 && h < 24) ? h : 0;
-
minute = (m >= 0 && m < 60) ? m : 0;
-
second = (s >= 0 && s < 60) ? s : 0;
-
}
-
-
// Print Time in military format
-
void Time::printMilitary()
-
{
-
cout << (hour < 10 ? "0" : "") << hour << ":"
-
<< (minute < 10 ? "0" : "") << minute << ":"
-
<< (second < 10 ? "0" : "") << second;
-
}
-
-
// Print time in standard format
-
void Time::printStandard()
-
{
-
cout << ((hour == 0 || hour == 12) ? 12 : hour % 12)
-
<< ":" << (minute < 10 ? "0" : "") << minute
-
<< ":" << (second < 10 ? "0" : "") << second
-
<< (hour < 12 ? " AM" : " PM");
-
}
-
-
// Driver to test simple class Time
-
int main()
-
{
-
string inputFile = "time.txt";
-
ifstream fin;
-
char line[256];
-
-
fin.open(inputFile.c_str ());
-
if (!fin)
-
{
-
cerr<< "Unable to open input file \n";
-
exit(2);
-
}
-
fin.getline(line, sizeof(line));
-
istringstream is(line);
-
int total = 0;
-
is>>total;
-
-
Time* t = new Time[total];
-
-
int hr = 0, min = 0, sec = 0;
-
char ch;
-
for(int i=0; i<total; ++i)
-
{
-
fin.getline(line, sizeof(line));
-
istringstream is(line);
-
is>>hr>>ch>>min>>ch>>sec;
-
t[i].setTime(hr, min, sec);
-
}
-
cout << "\n\nMilitary time after setTime is "<<endl;
-
for(int i=0; i<total; ++i)
-
{
-
t[i].printMilitary();
-
cout<<"\t";
-
}
-
cout << "\nStandard time after setTime is "<<endl;
-
for(int i=0; i<total; ++i)
-
{
-
t[i].printStandard();
-
cout<<"\t";
-
}
-
fin.close();
-
-
return 0;
-
}
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 - int index1 = 0, index2 = 0;
-
char op;
-
Time resultant;
-
while(!fin.eof())
-
{
-
fin.getline(line, sizeof(line));
-
istringstream is(line);
-
is>>index1>>op>>index2;
-
switch(op)
-
{
-
case '+':
-
resultant = t[index1] + t[index2];
-
resultant.setTime(resultant.getHr(), resultant.getMin(), resultant.getSec());
-
resultant.printMilitary();
-
cout<<endl;
-
resultant.printStandard();
-
cout<<endl;
-
break;
-
case '-':
-
resultant = t[index1] - t[index2];
-
resultant.setTime(resultant.getHr(), resultant.getMin(), resultant.getSec());
-
resultant.printMilitary();
-
cout<<endl;
-
resultant.printStandard();
-
cout<<endl;
-
break;
-
}
-
-
}
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: N3TB1N |
last post by:
Let me try again. I could use some help with this assignment, even
though my teacher does not grade assignments.but because I need to
know this stuff for a test very soon, but haven't been in class for
awhile and don't know what I'm doing.
I have included my (probably wrong) answers for the first few
questions. It would be great if...
|
by: Merlin |
last post by:
Hi
Been reading the GOF book and started to make the distinction between
Class and Interface inheritance. One question though:
Do pure abstract classes have representations? (data members?)
What about abstract classes?
Should abstract classes have a destructor and or constructor? What
about pure abstract classes?
|
by: mike |
last post by:
Hi,
I am having difficulty in creating a thread using pthread_create. It
seems that pthread_create does not execute 'program', and returns -1;
I have checked the API but I am not sure why this isn't working.
#include <stdio.h>
#include <pthread.h>
|
by: Carlos Lozano |
last post by:
Hi,
What is the right way to create an OCX COM component. The component is
already registerred, but can't create an instance. I am using the reference
to the interop module created.
If I use javascript or vbscript it works.
I will appreciate any help.
I do the following (C#):
|
by: Allen |
last post by:
I have a class that returns an arraylist. How do I fill a list box from what
is returned? It returns customers which is a arraylist but I cant seem to
get the stuff to fill a list box. I just learning and really need some help
bad.
Public Shared Function GetAll() As ArrayList
Dim dsCustomer As New DataSet()
Dim sqlQuery As String = "SELECT...
| |
by: Mats Lycken |
last post by:
Hi,
I'm creating a CMS that I would like to be plug-in based with different
plugins handling different kinds of content.
What I really want is to be able to load/unload plugins on the fly
without restarting the application.
What I did was to create an AppDomain that loaded the plugins and
everything was great, until I tried to pass...
|
by: Brian |
last post by:
I have a question that some may consider silly, but it has me a bit
stuck and I would appreciate some help in understanding what is going
on.
For example, lets say that I have a class that creates a student
object.
Class Student:
def setName(self, name)
self.name = name
|
by: MikeB |
last post by:
Hi, I'd appreciate some help, please. I'm writing a VS2005 VB project
for school and one of the requirements is that every screen should have
a "Help" button.
I could do it by writing a clumsy case statement like this:
sub showHelp(byval frm as String)
Select Case (frm)
Case "Form1"
dim help as new Form1
|
by: azzuri |
last post by:
Hi,
The title seems vague, but i could not think of anything more descriptive. I am creating a database and i am a bit stuck. I have started to create a database for pupils in a school.
I have created the tables, includes:
Pupil (containing pupil information, class etc)
Homework (containing homework information)
Complete (table...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it. ...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...
| |