Array within an array of structs?

Okay, so my last program calculated a preventive maintenance schedule for 6 machines based on input from a file. There were to be 6 reports - one per machine- generated.
Now I'm having trouble combining all 6 reports into just 1 report that is sorted by dates. I thought I could add an array of scheduled workdays to the struct array, but if you can, I don't seem to be doing it correctly. Can someone help me? I'm posting the original program, since that's the one that currently works. Is it possible to add another array within this larger array of structs to hold each machine's various maintenance days?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
#include <iostream>
#include <fstream>  //Input/output files
#include <string>   //For strings
#include <sstream>  //For stringstream()
#include <iomanip>  //For setw()
using namespace std;
//File descriptors
ifstream infile;
ofstream outfile;
//Declare constants
const int MAX_RECORDS = 6;

//This is a hierarchical struct since Date is a struct type within member list of MachineRecord
struct Date
{
    int month;
    int day;
    int year;
};
struct MachineRecord
{
    string machineNbr;
    string name;
    Date   InstallationDate;
    int    preventMaint;
    int    serviceTimeInDays;
};
//Create an array of machine records
MachineRecord pMSchedule[MAX_RECORDS];

//Prototypes
void DisplayHeader();
int  OpenInputOutput();
void ReadInData(string inputString, MachineRecord record[MAX_RECORDS]);
void FillStruct(string inputString, int count, MachineRecord record[MAX_RECORDS]);
void ConvertDateToJulian(int year, int month, int day, int pmDays, int serviceTimeInt );
void ConvertDateToGreg(int julianDateToConvert);
void CreateSchedule(int installDateJul, int pMCycle, int month, int day, int year, int serviceTime);

//Main program
int main()
{
    //Variable declarations
    string inputString;

    //Call functions
    DisplayHeader();
    OpenInputOutput();
    ReadInData(inputString, pMSchedule);

    //Close input/output files
    infile.close();
    outfile.close();
    return 0;//Quit
}//End of main program
//******************************************************************************
//Function reads from input file
void ReadInData(string inputString, MachineRecord record[MAX_RECORDS])
{//Passes in each line read in, and array of records
    //While in input file, read in all lines of data
    while (!infile.eof())
    {   //For all records
        for(int count = 0; count < MAX_RECORDS; count++)
        {
            getline(infile, inputString);//Get first line of input file
            FillStruct(inputString, count, pMSchedule);//Calls function to plug records into array
        }//Falls out of inner loop once all records have been read in
    }//Falls out of loop once at end of file
}//End of function
//******************************************************************************
//Function takes data and puts it into struct
void FillStruct(string currentString, int count, MachineRecord record[MAX_RECORDS])
{//Passes in each line read in, the counter, and array of records
    //Variables to hold string parts after each line is read-in
    string numbers;
    int monthInt;
    int dayInt;
    int yearInt;
    int pmInt;
    int serviceTimeInt;

    //Gets string value from positions 0-4
    record[count].machineNbr = currentString.substr(0, 5);//Assigns to record

    //Gets string value from positions 5-19
    record[count].name = currentString.substr(5, 15);//Assigns to record

    //Gets rest of string from position 20 on
    numbers = currentString.substr(20, 20);//Gets all numbers after machine name

    //Converts string to numerical ints that can be used in struct
    stringstream(numbers) >> monthInt >> dayInt  >> yearInt >> pmInt >> serviceTimeInt;
    record[count].InstallationDate.month = monthInt;//Assigns to record
    record[count].InstallationDate.day = dayInt;//Assigns to record
    record[count].InstallationDate.year = yearInt;//Assigns to record
    record[count].preventMaint = pmInt;//Assigns to record
    record[count].serviceTimeInDays = serviceTimeInt;//Assigns to record

    DisplayTitle();//Calls function

    //Displays name and number and pm stats
    cout << "________________________________________" << endl;
    cout << "Machine             :" << record[count].name << endl;
    cout << "Machine Number is   :" << record[count].machineNbr << endl;
    cout << "Needs maint. every  :" << record[count].preventMaint << " days" << endl;
    cout << "Service Time (days) :" << record[count].serviceTimeInDays << endl << endl;

    outfile << "________________________________________" << endl;
    outfile << "Machine             :" << record[count].name << endl;
    outfile << "Machine Number is   :" << record[count].machineNbr << endl;
    outfile << "Needs maint. every  :" << record[count].preventMaint << " days" << endl;
    outfile << "Service Time (days) :" << record[count].serviceTimeInDays << endl << endl;

    DisplayLabel();//Calls function
    ConvertDateToJulian(yearInt, monthInt, dayInt, pmInt, serviceTimeInt);//Calls function
}//End of function

//******************************************************************************
//Function to take date of installation and convert it to a julian date in order to calculate
//service schedule.
void ConvertDateToJulian(int year, int month, int day, int pmDays, int serviceTime)
{//Passes in Installation date
    //Declare variables to be used in converting date
    int julianDate;
    int I;
    int J;
    int K;
    I = year;
    J = month;
    K = day;

    //Formula to convert gregorian date to julian date
    julianDate = K - 32075 + 1461 *(I + 4800 +(J - 14)/ 12)/ 4 + 367 *
                 (J - 2 -(J - 14)/ 12 *12)/ 12 - 3 * ((I + 4900 +(J-14)/12)/100)/4;

    ConvertDateToGreg(julianDate);//Calls function
    CreateSchedule(julianDate, pmDays, J, K, I, serviceTime);//Calls function
}//End of function

//******************************************************************************
//Function converts Julian date back to Gregorian date
void  ConvertDateToGreg(int julianDateToConvert)
{//Passes in julian date
    int I;
    int J;
    int K;
    int L;
    int N;
    int YEAR;
    int MONTH;
    int DAY;

    //Formula to convert julian date to regular date (gregorian)
    L = julianDateToConvert + 68569;
    N = 4 * L/ 146097;
    L = L - (146097 * N + 3)/ 4;
    I = 4000 *(L + 1)/ 1461001;
    L = L - 1461 * I/ 4 + 31;
    J = 80 * L/ 2447;
    K = L - 2447 * J/ 80;
    L = J/ 11;
    J = J + 2 - 12 * L;
    I = 100 *(N - 49) + I + L;

    YEAR = I;
    MONTH = J;
    DAY = K;
    //Displays installation date and all pm days
    cout << setw(2) << MONTH << "-" << setw(2) << DAY << "-" << YEAR << endl;
    outfile << setw(2) << MONTH << "-" << setw(2) << DAY << "-" << YEAR << endl;
}//End of function

//******************************************************************************
//Function determines dates to schedule required preventive maintenance
void CreateSchedule(int installDateJul, int pMCycle, int month, int day, int year, int serviceTime)
{//Passes in julian install date, how often machine needs pm, and the date of last scheduled pm
    int scheduleDay;  //Variable to hold each maintenance day
    int fiveMonthsOut;//Variable to hold julian date for end of five months
    int today;        //Variable to hold julian date for March 1, 2012
    fiveMonthsOut = 2456141;//Julian date for Aug 1, 2012
    today = 2455988;  //Julian date for March 1.2012

    scheduleDay = installDateJul + pMCycle;//First pm date is set amount of days after install
    while (scheduleDay <= fiveMonthsOut)//While we are within 5 month period
    {
        while (scheduleDay < today)//While pm day is before today
        {
           scheduleDay = scheduleDay + pMCycle + serviceTime;//Get next pm day
        }
        ConvertDateToGreg(scheduleDay);//Call function when pm dayis at least March 1
        //Calculate next pm date by adding how many days between service
        //AND the # of days service takes
        scheduleDay = scheduleDay + pMCycle + serviceTime;
    }//Fall out of loop once scheduled date is after 5 month mark
}//End of function 
The reports generated with this:



     PREVENTIVE MAINTENANCE SCHEDULE
----------------------------------------
Scheduled workdays for the next 5 months
Beginning: March  1, 2012
Ending:    August 1, 2012
________________________________________
Machine             :Masher Basher
Machine Number is   :98273
Needs maint. every  :21 days
Service Time (days) :2

INSTALLATION DATE: 11-15-2011
 3- 7-2012
 3-30-2012
 4-22-2012
 5-15-2012
 6- 7-2012
 6-30-2012
 7-23-2012
----------------------------------------


     PREVENTIVE MAINTENANCE SCHEDULE
----------------------------------------
Scheduled workdays for the next 5 months
Beginning: March  1, 2012
Ending:    August 1, 2012
________________________________________
Machine             :Ittybit Pencher
Machine Number is   :83242
Needs maint. every  :18 days
Service Time (days) :1

INSTALLATION DATE: 12-22-2011
 3- 6-2012
 3-25-2012
 4-13-2012
 5- 2-2012
 5-21-2012
 6- 9-2012
 6-28-2012
 7-17-2012
----------------------------------------


     PREVENTIVE MAINTENANCE SCHEDULE
----------------------------------------
Scheduled workdays for the next 5 months
Beginning: March  1, 2012
Ending:    August 1, 2012
________________________________________
Machine             :Rackem Smacker
Machine Number is   :78023
Needs maint. every  :23 days
Service Time (days) :2

INSTALLATION DATE:  1-31-2012
 3-19-2012
 4-13-2012
 5- 8-2012
 6- 2-2012
 6-27-2012
 7-22-2012
----------------------------------------


     PREVENTIVE MAINTENANCE SCHEDULE
----------------------------------------
Scheduled workdays for the next 5 months
Beginning: March  1, 2012
Ending:    August 1, 2012
________________________________________
Machine             :Caper Bot
Machine Number is   :17381
Needs maint. every  :20 days
Service Time (days) :1

INSTALLATION DATE:  2-14-2012
 3- 5-2012
 3-26-2012
 4-16-2012
 5- 7-2012
 5-28-2012
 6-18-2012
 7- 9-2012
 7-30-2012
----------------------------------------


     PREVENTIVE MAINTENANCE SCHEDULE
----------------------------------------
Scheduled workdays for the next 5 months
Beginning: March  1, 2012
Ending:    August 1, 2012
________________________________________
Machine             :Screaming Remer
Machine Number is   :34820
Needs maint. every  :29 days
Service Time (days) :1

INSTALLATION DATE:  2-28-2012
 3-28-2012
 4-27-2012
 5-27-2012
 6-26-2012
 7-26-2012
----------------------------------------


     PREVENTIVE MAINTENANCE SCHEDULE
----------------------------------------
Scheduled workdays for the next 5 months
Beginning: March  1, 2012
Ending:    August 1, 2012
________________________________________
Machine             :Punch-a-momma
Machine Number is   :65312
Needs maint. every  :40 days
Service Time (days) :3

INSTALLATION DATE:  2-18-2012
 3-29-2012
 5-11-2012
 6-23-2012
----------------------------------------


I need a way to get all of these report dates into an array so I can sort them and print them by date and machine. How do I go about doing that?
closed account (zb0S216C)
ErinCorona wrote:
I need a way to get all of these report dates into an array so I can sort them and print them by date and machine. How do I go about doing that?

Since the amount of dates varies per schedule, you may have to dynamically allocate an array to match the size you want, or, use std::vector[1]; a dynamic array.

References:
[1] http://www.cplusplus.com/reference/stl/vector/


Wazzak
I'll research that right now, thank you.
Oh wait, but my instructions are to "store all the schedule dates in an array of structures and then sort them by date."

So I don't think we're supposed to use vectors. Can't I add a new member to the MachineRecord struct that is an array? I could title it workDays and make it an int, so that it could hold the julian date for each workday?

Then, I could just add to it when I calculate the dates for each machine? It would just be one big array? Then I could sort it...

That's what I've been trying to do but I'm having no luck. Is this possible using above restraints?
closed account (zb0S216C)
ErinCorona wrote:
Can't I add a new member to the MachineRecord struct that is an array?

Of course you can :) Do you mean something similar to this:

1
2
3
4
5
6
7
8
struct MachineRecord
{
    string machineNbr;
    string name;
    Date   workDays[MAX_NO_OF_DATES];
    int    preventMaint;
    int    serviceTimeInDays;
};


Wazzak
Yes. I have done that, and I think I set it up to sort it after that, but I've done something wrong, because when I print it out, all I have is a bunch of zeros. Here is what my code looks like for the two functions I've changed:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
int main()
{
    //Variable declarations
    string inputString;

    //Call functions
    OpenInputOutput();
    DisplayHeader();
    ReadInData(inputString, pMSchedule);
    BadSort(pMSchedule, outfile);
   
    //Close input/output files
    infile.close();
    outfile.close();

    return 0;//Quit
}//End of main program

//Function determines dates to schedule required preventive maintenance
void CreateSchedule(int installDateJul, int pMCycle, int month, int day, int year, int serviceTime, MachineRecord record[MAX_RECORDS], ofstream& outfile)
{//Passes in julian install date, how often machine needs pm, and the date of last scheduled pm
    int scheduleDay;  //Variable to hold each maintenance day
    int fiveMonthsOut;//Variable to hold julian date for end of five months
    int today;        //Variable to hold julian date for March 1, 2012
    fiveMonthsOut = 2456141;//Julian date for Aug 1, 2012
    today = 2455988;  //Julian date for March 1.2012
    int count = 0;
    int counter = 0;

    scheduleDay = installDateJul + pMCycle;//First pm date is set amount of days after install
    while (scheduleDay <= fiveMonthsOut)//While we are within 5 month period
    {
        while (scheduleDay < today)//While pm day is before today
        {
           scheduleDay = scheduleDay + pMCycle + serviceTime;//Get next pm day
        }
        record[count].workDays[counter] = scheduleDay;
        cout << "Workday is: " << scheduleDay << endl;
        outfile << "Workday is: " << scheduleDay << endl;
        ConvertDateToGreg(scheduleDay);//Call function when pm day is at least March 1
        //Calculate next pm date by adding how many days between service
        //AND the # of days service takes
        scheduleDay = scheduleDay + pMCycle + serviceTime;
    }//Fall out of loop once scheduled date is after 5 month mark
}//End of function

void BadSort(MachineRecord record[MAX_RECORDS], ofstream& outfile)
{
    int pass;
    int i;
    int temp = 0;
    for(pass = 0; pass < MAX_RECORDS; pass++)
        for(i = 0; i < MAX_DAYS; i++)
        {
            if (record[pass].workDays[i] > record[pass].workDays[i + 1])
            {
                temp = record[pass].workDays[i];
                record[pass].workDays[i] = record[pass].workDays[i + 1];
                record[pass].workDays[i + 1] = temp;
                cout << record[pass].workDays[i] << endl;
            }
        }
}


Can you see why I'm getting all zeros printing out from the BadSort function?
Topic archived. No new replies allowed.