Help with classes, dynamic arrays and pointers

Discussion in 'C++' started by foker, Oct 18, 2006.

  1. foker

    foker Guest

    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:


    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;
    foker, Oct 18, 2006
    #1
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. kelvSYC

    Arrays and Pointers to Arrays

    kelvSYC, Sep 26, 2003, in forum: C Programming
    Replies:
    2
    Views:
    371
  2. Replies:
    1
    Views:
    633
    -berlin.de
    Mar 28, 2005
  3. foker
    Replies:
    1
    Views:
    335
    red floyd
    Oct 19, 2006
  4. Philipp
    Replies:
    21
    Views:
    1,101
    Philipp
    Jan 20, 2009
  5. cerr

    pointers, pointers, pointers...

    cerr, Apr 7, 2011, in forum: C Programming
    Replies:
    12
    Views:
    651
Loading...

Share This Page