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
>> clock.colon
>> clock.minute
>> clock.colon
>> clock.second;
return fin;
}

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

red floyd

foker said:
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.

Why? Why not use a vector?
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++;



vector<string> elements;
for (string s; getline(fin,s); )
elements.push_back(s);

This way, you don't *care* how many elements there are.

Similarly, use an istringstream to parse the string.

for (vector<string>::iterator it = elements.begin();
it != elements.end();
++it)
{
replace_if(it->begin(), it->end(), ':', ' ');
istringstream is(*it);
is >> hours >> minutes >> seconds;
}

I'm sure someone can improve on that loop, but hey, it's clear what it's
doing.
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,901
Latest member
Noble71S45

Latest Threads

Top