Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Help with classes, dynamic arrays and pointers

Reply
Thread Tools

Help with classes, dynamic arrays and pointers

 
 
foker
Guest
Posts: n/a
 
      10-18-2006
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:(E-Mail Removed)

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

 
Reply With Quote
 
 
 
 
red floyd
Guest
Posts: n/a
 
      10-18-2006
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<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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
pointers, pointers, pointers... cerr C Programming 12 04-07-2011 11:17 PM
Multidimensional arrays and arrays of arrays Philipp Java 21 01-20-2009 08:33 AM
Help with classes, dynamic arrays and pointers foker C++ 0 10-18-2006 09:38 PM
Learning pointers and arrays of pointers With Files ketema@gmail.com C Programming 1 03-28-2005 03:51 AM
Arrays and Pointers to Arrays kelvSYC C Programming 2 09-26-2003 06:52 AM



Advertisments