Help with vectors

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • shoes2908
    New Member
    • Dec 2007
    • 4

    Help with vectors

    Ok, below I have taken parts of the program that I know can be fixed but I don't know how to do it. Is there a way to have the arrival to be one whole vector? or do I have to split it up like i did. It reads HH:MM.

    vector<short> arrival_h(20), arrival_m(20), depart_m(20), depart_h(20);
    vector<string> list_names;


    cin >> hour >> temp >> min;

    arrival_h[worker_num - 1] = hour;
    arrival_m[worker_num - 1] = min;

    cin >> hour >> temp >> min;

    depart_h[worker_num - 1] = hour;
    depart_m[worker_num - 1] = min;


    for ( short l = 0; l < list_names.size (); l++)
    {
    servd_h = depart_h[l] - arrival_h[l];
    servd_m = depart_m[l] - arrival_m[l];

    cout << '\n' + list_names[l] + ": ";
    cout << arrival_h[l] << ':' << arrival_m[l];
    cout << " " << depart_h[l] << ':' << depart_m[l];
    cout << " " << servd_h << ':' << servd_m;

    }
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    You might consider a class to contain your data:
    [code=cpp]
    class Time
    {
    private:
    unsigned short HH;
    unisgned short MM;
    public:
    //TODO: methods as needed
    };
    [/code]

    Then you could have a vector<Time>. You would write Time methods to accesss the hour and minute and an operator<< could supply the colon on the display.

    Doing it this way hides the fact that you are using unsigned shorts for your implementation. Later, if you need to redesign this class to, say, use a string to hold the data, then you will not need to change any of the code that uses the Time class. Your redesign would be limited to the Time methods.

    Comment

    Working...