How can I write this code ( sorry I can't understand)

Assignment: Write a program according to the following condition using the structure:
Display information about employees whose salaries are above average and are less than 30 years old.
Structure fields: Last name, First name, Job title, Salary, Date of birth
build your structure, as described.
make a container of those. perhaps it would help if the container used can be sorted easily?
populate the container. while doing this, you may calculate the average to save effort, or do it stand alone later (better design of do one thing in one function, but less efficient, design and efficiency are often at odds)
write your display code... your data is sorted now by the salary, so you find the first one that is above average, start there and iterate all the rest of the container. Print the ones where age is < 30.
you can either compute age from DOB or figure out what DOB is < 30 and work off DOB directly. Either one works.
Last edited on

Since I am a beginner also I tried this out with only a few people.
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
#include<string>
#include<vector>
#include<iostream>
#include <iomanip>   
using namespace std;


struct date {
    int d,m,y;
};

struct people{
	string lastname;
	string firstname;
	string job;
	int salary;
	date dob;
};

void show(people p)
{
cout<<left<<setw(9)<<p.firstname<<setw(13)<<p.lastname<<setw(15)<<p.job<<setw(5)<<"DateOfBirth= "<<p.dob.d<<"."<<p.dob.m<<"."<<p.dob.y<<"     Salary = "
<<p.salary<<endl;
}

int difference(date dt1, date dt2)
{
	const int monthDays[12]
    = { 31, 28, 31, 30, 31, 30, 
       31, 31, 30, 31, 30, 31 };
    long int n1 = dt1.y * 365 + dt1.d;
    for (int i = 0; i < dt1.m - 1; i++)
        n1 += monthDays[i];
    long int n2 = dt2.y * 365 + dt2.d;
    for (int i = 0; i < dt2.m - 1; i++)
        n2 += monthDays[i];
    return (n2 - n1);
}

int range(int f ,int l)
 {
    return (rand() % ((l-f)+1)) + f;
}


int main()
{
	srand(5000);
	vector<string> ln={"Smith","Jones","Kelly","Macnab","Peterson","Anderson","Trump","Putin","Thatcher","Davies","Wilson","Flemming","Richardson"};
	vector<string> fn={"Robert","Sally","James","Ann","Peter","Mary","Paul","Andrew","Charles","June","Wilma","Henry","Louis"};
	vector<string> job={"clerk","assistant","engineer","sales person","trimmer","painter","trouble shooter"};
	vector<people>p(13);
	for(int i=0;i<p.size();i++)
	{
		p[i].lastname=ln[range(0,p.size()-1)];
		p[i].firstname=fn[range(0,p.size()-1)];
		p[i].job=job[range(0,6)];
		p[i].salary=range(1000,2000);
		p[i].dob={range(1,25),range(1,12),range(1970,2000)};
		show(p[i]);
	}
	cout<<endl;
	
	cout<<"less than 30 years old and earning more than 1500"<<endl<<endl;
	date present={1,1,2021};
		for(int i=0;i<p.size();i++)
		{
		
			if (p[i].salary>1500 && difference(p[i].dob,present)/365 < 30) show(p[i]);
		}
		system("pause");
	
} 
Thanks oggin. You helped me a lot (:
Topic archived. No new replies allowed.