replacing the arrays by one array of structure

i have been scratching my head with this....as practice for my exam tomorrow im trying to change these functions into structures but dont know how to pass the arrays becasue they're going to be read from a text file. i dont know whether it should reside in the main or not. Help will be appreciated . thanks in advance
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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225

#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>


using namespace std;

int n;
const double FALL = 0.39,
             SPRING = 0.37,
             SUMMER = 0.24;
const int SIZE = 9;
const int SIZE2 = 9;
void inputdata(ifstream &, string[][SIZE], double[][3][3], double[][SIZE2], int, int);
void validatedata(string[][SIZE], int, int);
void validatedata(double[][3][3], int, int);
void fwe(double[][3][3], double[][SIZE2], int, int);
void tafwe(double [][SIZE2], int , int);
void srinpercent(double [][SIZE2], string [][SIZE], int , int );
void salary(double [][SIZE2], int , int );
void letter(string [][SIZE], double [][SIZE2], int , int );
void report(ofstream &, string [][SIZE], double [][3][3], double [][SIZE2], int , int );

int main()
{
    cout << "How many Employees? ";
    cin >> n;
    string mystring[n][SIZE];
    double grades[n][3][3];
    double totalval[n][SIZE2];

    while(n < 1 || n > 3)
    {
        cout << "Invalid! emp must range from 1 - 3" << endl;
        cout << "How many Employees? ";
        cin >> n;
    }
    ifstream fin;
    fin.open("project1.txt");
    if(!fin)
    {
        cout << "error" << endl;
        return 1;
    }
    ofstream fout;
    fout.open("project2.txt");

    for(int emp = 0;emp < n; emp++)
    {
        inputdata(fin, mystring, grades, totalval, n, emp);
        validatedata(mystring, n, emp);
        validatedata(grades, n, emp);
        fwe(grades, totalval, n, emp);
        tafwe(totalval, n, emp);
        srinpercent(totalval, mystring, n, emp);
        salary(totalval, n, emp);
        letter(mystring, totalval, n, emp);
        report(fout, mystring, grades, totalval, n, emp);
    }
    fin.close();
    fout.close();
    return 0;

}


void inputData(ifstream &fin, string mystring[][SIZE], double grades[][3][3], double totalval[][SIZE2], int n, int emp)
{
    for(int i = 0;i < SIZE;i++)
    {
        getline(fin, mystring[emp][i]);
    }
        for(int year =0; year < 3; year++)
        {
            for(int j = 0;j < 3;j++)
            {
                fin >> grades[emp][year][j];
            }
         }

    fin >> totalval[emp][5];
    fin.ignore();
}

void validateData(string mystring[][SIZE], int n, int emp)
{
    //sending the information from the input file to the cout file with the loop
    for(int i = 0;i < SIZE;i++)
    {
        //inner loop for non numeric strings
        if(mystring[emp][i].length() < 1 || mystring[emp][i].length() > 100)
        {
            cout << "error" << endl;
            continue;
        }
}
}

void validateData(double grades[][3][3], int n, int emp)
{
    //reads the numeric values from the input file
    for(int year = 0; year < 3; year++)
    {
       for(int j = 0;j < 3;j++)
        {
            for(int k = 0;k < 3;k++)
            {
                if(grades[emp][year][j] < 1 || grades[emp][year][j] > 1000)
                {
                    cout << "error";
                    break;
                }
            }
        }
    }

}

void fwe(double grades[][3][3], double totalval[][SIZE2], int n, int emp)
{
    totalval[emp][0] = (grades[emp][0][0] * SPRING) + (grades[emp][0][1] * SUMMER) + (grades[emp][0][1] * FALL);
    totalval[emp][1] = (grades[emp][1][2] * SPRING) + (grades[emp][1][1] * SUMMER) + (grades[emp][1][2] * FALL);
    totalval[emp][2] = (grades[emp][2][0] * SPRING) + (grades[emp][2][1] * SUMMER) + (grades[emp][2][2] * FALL);
    totalval[emp][3] = totalval[emp][0] + totalval[emp][1] + totalval[emp][2];
}

void tafwe(double totalval[][SIZE2], int n, int emp)
{
    totalval[emp][3] = totalval[emp][0] + totalval[emp][1] +totalval[emp][2];
    totalval[emp][4] = totalval[emp][3]/3;
}

void srinpercent(double totalval[][SIZE2], string mystring[][SIZE], int n, int emp)
{
    if(totalval[emp][4] < 75.)
    {
        totalval[emp][6] = 0;
    }
    else if(totalval[emp][4] > 75 && totalval[emp][4] <= 80)
    {
        totalval[emp][6] = 1;
    }
    else if(totalval[emp][4] > 80 && totalval[emp][4] <= 90)
    {
        totalval[emp][6] = 3;
    }
    else if(totalval[emp][4] > 90 && totalval[emp][4] <= 100)
    {
        totalval[emp][6] = 5;
    }
    else if(totalval[emp][4] > 100)
    {
        totalval[emp][6] = 10;
    }
}

void salary(double totalval[][SIZE2], int n, int emp)
{
    totalval[emp][7] = (totalval[emp][6]/100) * totalval[emp][5];
    totalval[emp][8] = totalval[emp][5] + totalval[emp][7];
}

void letter(string mystring[][SIZE], double totalval[][SIZE2], int n, int emp)
{
    if(totalval[emp][4] < 70)
    {
        mystring[emp][7] = "Warning!";
    }

    else if(totalval[emp][4] >= 95)
    {
        mystring[emp][8] = "Congrats!";
    }
}

void report(ofstream &fout, string mystring[][SIZE], double grades[][3][3], double totalval[][SIZE2], int n, int emp)
{
    fout << mystring[emp][0] << endl;
    fout << "\t" << mystring[emp][1] << endl;
    fout << "Name: " << mystring[emp][2] << endl;
    fout << "Supervisor:" << mystring[emp][3] << endl;
    fout << "ID: " << mystring[emp][4] << endl;
    fout << "num: " << mystring[emp][5] << endl;
    fout << "addy: " << mystring[emp][6] << endl;

     for(int year = 0;year < 3;year++)
        {
            for(int j = 0;j < 3;j++)
            {
                switch(j)
                {
                    case 0: //case for 2013
                        fout << " Spring Semester Evaluation, " << fixed << setprecision(2) << 2015 + year << ": " << grades[emp][year][j] << endl;
                        break;
                    case 1: //case for 2014
                        fout << " Summer Semester Evaluation, " << fixed << setprecision(2) << 2015 + year << ": " << grades[emp][year][j] << endl;
                        break;
                    case 2: //case for 2015
                        fout << "   Fall Semester Evaluation, " << fixed << setprecision(2) << 2015 + year << ": " << grades[emp][year][j] << endl;
                        break;
                }
            }
                fout << "final, 2015: " << totalval[emp][0] << endl;
                fout << "final, 2016: " << totalval[emp][1] << endl;
                fout << "final, 2017: " << totalval[emp][2] << endl;
                fout << "total: " << totalval[emp][3] << endl;
                fout << "Average: " << totalval[emp][4] << endl;
                fout << "salary: " << totalval[emp][5] << endl;
                fout << "in percent: " << totalval[emp][6] << endl;
                fout << "in dollar: " << totalval[emp][7] << endl;
                fout << "both: " << totalval[emp][8] << endl;
                if(totalval[emp][4] < 70)
                {
                    fout << mystring [emp][7] << endl;
                    fout << endl;
                }
                else if(totalval[emp][4] >= 95)
                {
                    fout << mystring[emp][8] << endl;
                }
        }
}
Last edited on
Your code is unreadable.
You should edit your post and re-paste the code in code tags: http://www.cplusplus.com/forum/articles/16853/
My bad, its fixed now
I don't know what it means to change functions into structures.
Do you mean you want the functions to receive structs as parameters instead of individual values?
Do you mean you want to make a class or struct and move the functions into it as members?
What would your struct/class be called?
What would be in it?
Last edited on
i mean to replace all arrays by one array structures this is my new code
i dont know how to apply the structures to these void funtions

1
2
3
4
5
6
7
8
9
void inputdata(ifstream &, string[][SIZE], double[][3][3], double[][SIZE2], int, int);
void validatedata(string[][SIZE], int, int);
void validatedata(double[][3][3], int, int);
void fwe(double[][3][3], double[][SIZE2], int, int);
void tafwe(double [][SIZE2], int , int);
void srinpercent(double [][SIZE2], string [][SIZE], int , int );
void salary(double [][SIZE2], int , int );
void letter(string [][SIZE], double [][SIZE2], int , int );
void report(ofstream &, string [][SIZE], double [][3][3], double [][SIZE2], int , int );

especially cuz im reading from a txt file

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

void inputdata(ifstream, double[][3][3], struct employeeinfo);

struct address
{
    string street,
           city,
           state,
           zip;
};
struct semester
{
    double evaluation[9];
};

struct computed_evaluation
{
    double final_ev[3],
           total_final,
           av_final;
};

struct employeeinfo
{
    string head1,
           head2,
           employee,
           supervisor,
           id,
           num;
    address employee_address;
    semester input_evaluation;
    computed_evaluation comp_eval;
    double salary,
           sraisep,
           sraised,
           sraisedr;
    string warn,
           note;

};

void validatedata(ifstream &input, double grades[][3][3], struct employeeinfo employee)
{

}

int main()
{
    int n;
    cout << "Enter Number of Employees: ";
    cin >> n;
    while(n < 1 || n > 3)
    {
        cout << "Invalid! emp must range from 1 - 3" << endl;
        cout << "How many Employees? ";
        cin >> n;
    }
    ifstream input;
    input.open("project1.txt");
    if(!input)
    {
        cout << "error" << endl;
        return 1;
    }
    ofstream output;
    output.open("project2.txt");

}
Last edited on
I can't comprehend how you are mapping these to each other.

What does string mystring[][SIZE] in the original function hold?
What about double totalval[][SIZE2] ?

Post some of the input data and describe it's meaning. It seems weird that you have the word "employee" and also "grades" which sounds like it applies to students.
Last edited on
ok sorry i wasn't clear

so in a previous example we were to read files from an input file which states an employees information and evaluation for fall, spring, and summer

• Function main: It will include proper declarations. It will read the value of n (the number of employees) from the key board and validate using a while loop (The value of n must be at least 3). The outer for loop on employee number will be in this function. It will call other functions as needed.
• Function inputData: This function will read the data from the input file into proper arrays and then call ValidateData to validate.
• Functions validateData: These will be overloaded functions with same name. One function will validate non-numeric data and the other will validate numeric data.
• Function FWE: This function will compute and store into proper array elements the Final Weighted Evaluation and it will be called for each year.
• Function TAFWE: This function will compute and store into proper array elements the Total and Average Final Weighted Evaluations
• Function SRinPercent: This function will compute and store into proper array elements the Salary Raise in %.
• Function Salary: This function will compute and store in proper array elements the Salary Raise in Dollars and the Salary in dollars with the raise.
• Function Letter: This function will determine and write Warning/Appreciation Letter and store it in proper array elements.
• Function Report: After all computations are completed. Arrays will contain all the information. Pass these arrays to this function which will write to the output file the reports of all employees.

But now its asking to replace all the arrays that were created to match each line in the input file and replace them into one array structures.

this is an example of the first employee input file

INPUT
Name of the Employee: Nancy Price
Name of the Supervisor: John Albert
Employee ID #: A-5971-359182
Telephone Number: (512) 354 – 9854
Address: 530 Albatross Dr., California, TX 78489
Spring Semester Evaluation, 2011: 80.00
Summer Semester Evaluation, 2011: 75.00
Fall Semester Evaluation, 2011: 90.00
Spring Semester Evaluation, 2012: 83.00
Summer Semester Evaluation, 2012: 78.00
Fall Semester Evaluation, 2012: 93.00
Spring Semester Evaluation, 2013: 86.00
Summer Semester Evaluation, 2013: 79.00
Fall Semester Evaluation, 2013: 95.00
Current Salary: $90,000.00

and the output should look like

OUTPUT File

Employee Report
Annual Employee Evaluation Report Sept. 1, 2013 to August 31, 2014

Name of the Employee: Nancy Price
Name of the Supervisor: John Albert
Employee ID #: A-5971-359182
Telephone Number: (512) 354 – 9854
Address: 510 Albatross Dr., California, TX 78489
Spring Semester Evaluation, 2011: 80.00
Summer Semester Evaluation, 2011: 75.00
Fall Semester Evaluation, 2011: 90.00
Spring Semester Evaluation, 2012: 83.00
Summer Semester Evaluation, 2012: 78.00
Fall Semester Evaluation, 2012: 93.00
Spring Semester Evaluation, 2013: 86.00
Summer Semester Evaluation, 2013: 79.00
Fall Semester Evaluation, 2013: 95.00
Final Weighted Evaluation, 2011: Computed Value
Final Weighted Evaluation, 2012: Computed Value
Final Weighted Evaluation, 2013: Computed Value
Total Final Weighted Evaluation: Computed Value
Average Final Weighted Evaluation: Computed Value
Current Salary: $90,000.00
Salary Raise in %: Computed Value
Salary Raise in Dollars: Computed Value
Salary in dollars with the raise: Computed Value


Warning/Appreciation Letter: Drafted by the student

Note: This report for Nancy Price was prepared according to the fair practice of the University.
Any discrepancies must be reported by Nancy Price to her supervisor, John Albert.

Topic archived. No new replies allowed.