473,406 Members | 2,208 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Help with classes, dynamic arrays and pointers

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++;

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:ag**********@hotmail.com

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])
i++;
numElements = i;

for(i=0; i<maxSize;i++)
{
fout << array[i] << 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;
}

Oct 18 '06 #1
1 2031
foker wrote:
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])
i++;

vector<stringelements;
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.
Oct 18 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Greg Baker | last post by:
I don't know what standard protocol is in this newsgroup. Am I allowed to post code and ask for help? I hope so.. :) Here's my problem: I am trying problem 127 of the valladolid online...
3
by: Josh | last post by:
Howdy i was recently given a program to do. I have to create a 2d matrix with pointers i have the whole idea down with pointers but there is a problem with one of them i have the code written down...
13
by: Ben | last post by:
I have a program which is using a lot of memory. At the moment I store a lot of pointers to objects in std::vector. (millions of them) I have three questions: 1) Lets say the average Vector...
1
by: Geoff | last post by:
I was wondering if anyone could help me with a problem I am having. I am trying to read in a list of numbers from a file and then sort them using pointers and malloc() and free(). I know how to...
23
by: vinod.bhavnani | last post by:
Hello all, I need desperate help Here is the problem: My problem today is with multidimensional arrays. Lets say i have an array A this is a 4 dimensional static array.
0
by: foker | last post by:
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...
4
by: hobbes992 | last post by:
Howdy folks, I've been working on a c project, compiling using gcc, and I've reached a problem. The assignment requires creation of a two-level directory file system. No files have to be added or...
2
by: StevenChiasson | last post by:
For the record, not a student, just someone attempting to learn C++. Anyway, the problem I'm having right now is the member function detAddress, of object controller. This is more or less, your...
8
by: Oliver Graeser | last post by:
Hi All, I'm coming from Java to C++ and this is one of the very last problems I have so far... In Java, if I have, say, a class SISNode that extends NetworkNode, I can have a function that...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
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,...
0
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...
0
jinu1996
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.