Help with classes, dynamic arrays and pointers

F

foker

I have a file with a list of times in it in the form hh:mm:ss. The file

starts with a single INT like 460. That int is the number of times in
the file so I can size my dynamic array correctly. I had to create my
array as a type string because of the colons in it. I need to be able
to save it in type INT so I can do addition and subtraction on it. I
want to be able to add 2 times together or subtract 2 times and get a
new time.

string *array;
int i=0, numElements, maxSize;
fin >> maxSize;
array = new string [maxSize];
while(i<maxSize && fin >> array)
i++;


I realize I need to overload the + and - operators but I'm not sure how

to go about it. If someone would like to help me and has msn I may be
able to explain this better and have it resolved a bit quicker.
msn:[email protected]


My cpp file:
#include "time.h"


int main()
{
string input_file, output_file;
MyClock clock;


//Opening files and doing error checking
ifstream fin;
cout << "Enter the name of the input file: ";
cin >> input_file;
fin.open(input_file.c_str());
if(fin.fail())
{
cerr << "Could not open the input file.";
exit(1);
}
ofstream fout;
cout << "Enter the name of the output file: ";
cin >> output_file;
fout.open(output_file.c_str());
if(fout.fail())
{
cerr << "Could not open the output file.";
exit(2);
}


//Dynamic Array Tests
string *array;
int i=0, numElements, maxSize;
fin >> maxSize;
array = new string [maxSize];
while(i<maxSize && fin >> array)
i++;
numElements = i;


for(i=0; i<maxSize;i++)
{
fout << array << endl;
}


return 0;



}


My header file:
#pragma once;
#include <fstream>
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;

class MyClock
{
public:
//Constructor
MyClock(){hour=minute=second=0;};
//Destructor
//~MyClock();


//Accessors
/*
int setTime(int hour, int minute, int second);
int getTime(int& hour, int& minute, int& second);
*/


//Overloaded Operators
MyClock operator+(const MyClock&) const;
friend ostream& operator<<(ostream& fout,
const MyClock &clock);
friend istream& operator>>(istream& fin,
MyClock &clock);
friend bool operator<(const MyClock& left,
const MyClock &right);
friend bool operator>(const MyClock& left,
const MyClock &right);
friend bool operator<=(const MyClock& left,
const MyClock &right);
friend bool operator>=(const MyClock& left,
const MyClock &right);
friend bool operator==(const MyClock& left,
const MyClock &right);
friend bool operator!=(const MyClock& left,
const MyClock &right);


private:
int hour, minute, second, index1, index2;
char colon, symbol;



};


Other cpp file with implementation:
#include "time.h"

//Class implementation
/*
MyClock::~MyClock()
{
delete [] array;



}


*/

//istream, read in everything from the file
istream& operator>>(istream& fin,
MyClock &clock)
{
fin >> clock.hour return fin;



}


ostream& operator<<(ostream& fout,
const MyClock &clock)
{
fout << clock.hour << clock.colon << clock.minute <<
clock.colon <<
clock.second
<< endl;
return fout;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top