Structures

can someone tell me whats wrong with my code. Im a beginner. i am to read from an input file and store them line by line with structures. i know this kind of looks messy but it isn't compiling.


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
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>

using namespace std;

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

struct address
{
    string street;
};
struct semester
{
    double evaluation[9];
};

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

struct employeeinfo
{
    string head1,
           head2,
           name,
           supervisor,
           id,
           number;
    address employee_address;
    semester input_evaluation;
    computed evaluation comp_eval;
    double salary,
           sraisep,
           sraised,
           sraisedr;
    string warn,
           note;
};

void inputdata(ifstream &input, struct employeeinfo employee[], int n, int emp)
{
    for(int i = 0;i < 8;i++)
    {
        getline(input, employee[emp].mystring[i]);
    }
    for(int year = 0; year < 3;year++)
    {
        for(int j = 0;j < 3;j++)
        {
            input >> employee[emp].semester evaluation[year][j];
        }
    }
    input >> employee[emp].evauation[5];
    input.ignore();
}

void validatedata(struct employeeinfo employee[], int n, int emp)
{
    for(int i = 0;i < 9;i++)
    {
        if(employee[emp].mystring[i].length() < 1 || employee[emp].mystring[i].length() > 100)
        {
            cout << "Error" << endl;
            continue;
        }
    }
}

void validatedata(struct emplyeeinfo employee[], int n, int emp)
{
    for(int year = 0;year < 3;year++)
    {
        for(int j = 0; j < 3;j++)
        {
            for(int k = 0;k <3;k++)
            {
                if(employee[emp].grades[year][j] < 1 || employee[emp].grades[year][j] > 1000)
                {
                    cout << "ERROR. TOO LONG";
                    break;
                }
            }
        }
    }
}

void fwe(struct employeeinfo employee[], int n, int emp)
{
    employee[emp].evauation[0] = (employee[emp].grades[0][0] * SPRING) + (employee[emp].grades[0][1] * SUMMER) + (employee[emp].grades[0][1] * FALL);
    employee[emp].evauation[1] = (employee[emp].grades[1][2] * SPRING) + (employee[emp].grades[1][1] * SUMMER) + (employee[emp].grades[1][2] * FALL);
    employee[emp].evauation[2] = (employee[emp].grades[2][0] * SPRING) + (employee[emp].grades[2][1] * SUMMER) + (employee[emp].grades[2][2] * FALL);
    employee[emp].evauation[3] = employee[emp].evauation[0] + evauation[emp][1] + employee[emp].evauation[2];
}
void tafwe(struct employeeinfo employee[], int n, int emp)
{
    employee[emp].evauation[3] = employee[emp].evauation[0] + evauation[emp][1] + employee[emp].evauation[2];
    employee[emp].evauation[4] = employee[emp].evauation[3]/3;
}

void srinpercent(struct employeeinfo employee[], int n, int emp)
{
if(employee[emp].evauation[4] < 75.)
{
employee[emp].evauation[6] = 0;
}
else if(employee[emp].evauation[4] > 75 && employee[emp].evauation[4] <= 80)
{
employee[emp].evauation[6] = 1;
}
else if(employee[emp].evauation[4] > 80 && employee[emp].evauation[4] <= 90)
{
employee[emp].evauation[6] = 3;
}
else if(employee[emp].evauation[4] > 90 && employee[emp].evauation[4] <= 100)
{
employee[emp].evauation[6] = 5;
}
else if(employee[emp].evauation[4] > 100)
{
employee[emp].evauation[6] = 10;
}
}

void srinpercent(struct Employee employees[], int n, int emp)
{
if(employees[emp].evauation[4] < 75.)
{
employees[emp].evauation[6] = 0;
}
else if(employees[emp].evauation[4] > 75 && employees[emp].evauation[4] <= 80)
{
employees[emp].evauation[6] = 1;
}
else if(employees[emp].evauation[4] > 80 && employees[emp].evauation[4] <= 90)
{
employees[emp].evauation[6] = 3;
}
else if(employees[emp].evauation[4] > 90 && employees[emp].evauation[4] <= 100)
{
employees[emp].evauation[6] = 5;
}
else if(employees[emp].evauation[4] > 100)
{
employees[emp].evauation[6] = 10;
}
}
void srinpercent(struct Employee employees[], int n, int emp)
{
if(employees[emp].evauation[4] < 75.)
{
employees[emp].evauation[6] = 0;
}
else if(employees[emp].evauation[4] > 75 && employees[emp].evauation[4] <= 80)
{
employees[emp].evauation[6] = 1;
}
else if(employees[emp].evauation[4] > 80 && employees[emp].evauation[4] <= 90)
{
employees[emp].evauation[6] = 3;
}
else if(employees[emp].evauation[4] > 90 && employees[emp].evauation[4] <= 100)
{
employees[emp].evauation[6] = 5;
}
else if(employees[emp].evauation[4] > 100)
{
employees[emp].evauation[6] = 10;
}
}

void salary(struct Employee employees[], int n, int emp)
{
employees[emp].evauation[7] = (employees[emp].evauation[6]/100) * employees[emp].evauation[5];
employees[emp].evauation[8] = employees[emp].evauation[5] + employees[emp].evauation[7];
}

void letter(struct Employee employees[], int n, int emp)
{
if(employees[emp].evauation[4] < 70)
{
employees[emp].mystring[7] = "Warning!";
}

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

void report(ofstream &fout, struct Employee employees[], int n, int emp)
{
fout << employees[emp].mystring[0] << endl;
fout << "\t" << employees[emp].mystring[1] << endl;
fout << "Name: " << employees[emp].mystring[2] << endl;
fout << "Supervisor:" << employees[emp].mystring[3] << endl;
fout << "ID: " << employees[emp].mystring[4] << endl;
fout << "num: " << employees[emp].mystring[5] << endl;
fout << "addy: " << employees[emp].mystring[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 << ": " << employees[emp].grades[year][j] << endl;
break;
case 1: //case for 2014
fout << " Summer Semester Evaluation, " << fixed << setprecision(2) << 2015 + year << ": " << employees[emp].grades[year][j] << endl;
break;
case 2: //case for 2015
fout << " Fall Semester Evaluation, " << fixed << setprecision(2) << 2015 + year << ": " << employees[emp].grades[year][j] << endl;
break;
}
}
fout << "final, 2015: " << employees[emp].evauation[0] << endl;
fout << "final, 2016: " << employees[emp].evauation[1] << endl;
fout << "final, 2017: " << employees[emp].evauation[2] << endl;
fout << "total: " << employees[emp].evauation[3] << endl;
fout << "Average: " << employees[emp].evauation[4] << endl;
fout << "salary: " << employees[emp].evauation[5] << endl;
fout << "in percent: " << employees[emp].evauation[6] << endl;
fout << "in dollar: " << employees[emp].evauation[7] << endl;
fout << "both: " << employees[emp].evauation[8] << endl;
if(employees[emp].evauation[4] < 70)
{
fout << employees[emp].mystring[7] << endl;
fout << endl;
}
else if(employees[emp].evauation[4] >= 95)
{
fout << employees[emp].mystring[8] << endl;
}
}
}

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

    for(int emp = 0;emp < n; emp++)
    {
        inputdata(input, employee, n, emp);
        validatedata(employee, n, emp);
        fwe(employee, n, emp);
        tafwe(employee, n, emp);
    }
    input.close();
    output.close();
    return 0;
}
reading from this input file

INPUT
Name of the Employee: Nancy Price
Name of the Supervisor: John Albert
Employee ID #: A-5971-359182
Telephone Number: (512) 354 – 9855
Address: 510 Albatross Dr., Austin, TX 78310
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 this when asked to enter the number of employees. this is for one employee

OUTPUT

Name of the Employee: Nancy Price
Name of the Supervisor: John Albert
Employee ID #: A-5971-359182
Telephone Number: (512) 354 – 9855
Address: 510 Albatross Dr., Austin, TX 78310
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.
When I put your code into my compiler, nearly all of it was underlined in red. I must say it was rather lovely. There is only one thing for it. Scrap the project and start over. This time... do only a small portion of the assignment. Make sure that works and then move on to the next part. For example... write a program that just reads your text document first.

But here are some tips to help you. First a variable name must not contain any spaces. You cant haveint this is my int
It must beint this_is_my_int for example. Try not to have so many different structs. Use only as many structs as is actually needed. In other words, some of those structs could be combined. And don't try to use member variables that don't exist. For example struct employeeinfo does not have a member string variable called mystring.
Hello beemo,

Does your input file look exactly like what is in your second post or is it different?. Please post your exact input file so I can check if you are reading the file correctly. You can not write a program that reads a file without knowing what is in the file and how it is laid out.

In your program you have the function "validatedata" data twice with the same return value and the same parameters. The compiler will consider this an error because it will not know which function to use. You either need to rename one of the functions or delete one of them. Not sure which one to delete yet until I can see what other errors there are.

It is hard to tell right now with the way your code is not indented, but it looks like you are misusing the {}s. and it looks like some are in the wrong place.

Watch your indenting it really helps.

While I am here:

It is ALWAYS a good practice and programming to initialize your variables. If your compiler is using the C++11 standards or after the easiest way to initialize variables is with empty {}s, e.g., int num{};. This will initialize the variable to 0 (zero) or 0.0 for a double. A "char" will be initialized to "\0". "std::string"s are empty to start with and do not need to be initialized. Should you need to you can put a number between the {}s.You can also Initialize an array, e.g., int aNumbers[10]{};. This will initialize all elements of the array to 0 (zero). A use of
int aNumbers[10]{ 1 }; will initial the first element to "1" and the rest of the array to "0".. Following the "1" with ", 2 ..." will initialize the array with the numbers that you have used up to the entire array.

I will know when I get the program loaded up.

Hope that helps,

Andy
does the input file actually look like that? If so, that's difficult to work with. Should just have the raw data instead, without any random spaced labels. As it stands now, it's that much harder to parse through before finding the the actual piece of data.
Nancy Price
John Albert
A-5971-359182
512) 354 – 9855
510 Albatross Dr., Austin, TX 78310	
80.00
75.00

etc.
Hello beemo,

Like Manga has said I also received many things underlined in red, but I did not think it was that lovely because I saw a lot of work. Just my opinion.

I also agree that it would be best to save the original code to a different file and start over. It is not that the code is wrong it is more of the way you did some things.

An example I grabbed form the first "inputdata" function: employee[emp].mystring[i]. You have constructed an object of "employeeinfo", but not where you should have in main. An array of "mystring" is not defined in the struct "employeeinfo".

Some helpful hints:

Try not to use global variables unless they begin with "const" or "constexpr". Using a global like "n" it is so easy to change anywhere in your program that is is hard to find. It is better to define the variables in main and pass them to the functions that need them. Also when you look at a variable called "n" what is it used for? The name tells you nothing. It is better to use a name that is more descriptive of what it is or what it does. It is better to use a single letter variable as a for loop counter where it is contained in a small block.

When naming a variable it is best to start the name with a lower case letter lime "name" and if you have something a bit longer it is best to use the camel case method of "firstName" where the capital letter takes the place of a space. In the old days of DOS a space was designated by the use of the '_' underscore. This is acceptable, but generally avoided.

This may be my personal preference, but for functions, classes and structs I like to start the name with a capital letter so I know the difference between these and a regular variable.

You have already demonstrated that you have written your constant variables with capital letters. This is good. The part I would do differently is put each variable on a separate line.

The struct of "address" you really do not need because it should be in the "employeeinfo" struct where it belongs. You are just making more work that you do not need.

The struct "computed" may or may not be worth using. I need to get into the program more.

Let us start with what the input file looks like followed by the function to read the file. Until you have this working the rest of the program is useless for now.

Hope that helps,

Andy
Hello beemo,

Sorry it was late last night when I finished and did not get a chance to post this.

This is an example of what is said about small steps.

main:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <iomanip>  // <--- May not be needed in main.
#include <fstream>
#include <string>
#include <vector>

#include "Structs.hpp"

const double FALL = 0.39;
constexpr double SPRING = 0.37;
constexpr double SUMMER = 0.24;

void ReadFile(std::vector<EmployeeInfo>& employeeInfo);

int main()
{
	std::vector<EmployeeInfo> employeeInfo;

	ReadFile(employeeInfo);

	return 0;
}  //  End main 


These are the structs I used. I changed them a little based on what I had to work with. I like to put classes and structs in a separate header file and code for different functions in its own file. I called the file "Structs.hpp", but you can use any name that you want:
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
#ifndef _STRUCT_ 
#define _STRUCT_

struct Semester
{
	std::string s_semester;
	int s_year{};
	double s_evaluation{};
};

struct Computed
{
	double final_ev[3]{};
	double total_final{};
	double av_final{};
};

struct EmployeeInfo
{
	std::string s_head1;
	std::string s_head2;
	std::string s_name;
	std::string s_supervisor;
	std::string s_id;
	std::string s_phoneNumber;
	std::string s_address;
	std::string warn;
	std::string note;

	std::vector<Semester> s_semesters;

	computed evaluation, comp_eval;  // <--- Added ,
	double s_salary{};
	double s_sraisep{};
	double s_sraised{};
	double s_sraisedr{};
};

#endif // end !_STRUCT_ 

The "Computed" struct I have not done anything with yet because the program is not there yet. It is likely to change when the time is needed.

I have left out the "Read" function because I do not know what the input file looks like and there is a good chance that what I have done will be completely wrong and I do not want to mislead you with the wrong code.

Hope that helps,

Andy
Name of the Employee: Nancy Price


There is the chance that the input file looks like this, and to some this seems problematic. But I think this could work just fine. There is no need for this program, which I think might be some class assignment, to separate "Nancy Price" from the rest of the line. Save it all!

string name = "Name of the Employee: Nancy Price";

cout << name; This works.
Topic archived. No new replies allowed.