Criteria enforcing programme

Write your question here.

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


The file format the program wih from read


  First Name : john
  Last Name : peter
     Gender : M
 Student ID : 7483289
Course Code : 20174893
       Year : 2017
The Applicants has the Following Grades
     Math  	               20
  English                      38
  English literature           54
  Civic Ed                     60
  Accounts                     58
  Science                      40
  Biology                      39
  Accounts                     40

  First Name : Lisa
  Last Name : McSon
     Gender : F
 Student ID : 3674765
Course Code : 20813672
       Year : 2018
The Applicants has the Following Grades
  Math                          70
  English                       34
  Pure Math                     67
  Civic Ed                      88
  History                       54
  Physics                       50
  Chemistry                     67
  Biology                       56
  

  First Name : Chitalu
  Last Name : Malama
     Gender : F
 Student ID : 4672762
Course Code : 20146437
       Year : 2014
The Applicants has the Following Grades
  Math                         59
  English                      50
  Sicence                      88
  Food and Nutrition           45
  Biology                      56
  Religious Education          60
  Geography                    55
  



  First Name : Malika
  Last Name : Chili
     Gender : F
 Student ID : 4729208
Course Code : 20137346
       Year : 2013
The Applicants has the Following Grades
  Math                         45
  English                      34
  Science                      56
  Art & Music                  34
  Biology                      56
  History                      49
  Civic Ed                     37

First Name : :Leo
  Last Name : Chili
     Gender : M
 Student ID : 4825218
Course Code : 20207346
       Year : 2013
The Applicants has the Following Grades
  Busniess Math                65
  Math                         80
  English                      74
  Science                      66
  Technology Studies           64
  I.T                          56
  History                      79
  Civic Ed                     87  


This program is supposed to read grades of student from a file then calculate for the grades for those who meet the following criteria

1) Math,English and science must be greater then 50.
2) Any other 3 subjects must be greater then 50 so a student must six plus the three in the above condition.

For same reason the institution decides that a one can qualify if the he or she has better grades in substitute subjects as follows
if Pure Math greater than Math
if English literature grade is greater English
if is Science is not part of the subjects
Physics and Chemistry are considered.

I need help with the code...otherwise comment with progressive ideas

The first task is to read and parse the data into an appropriate container/structure.

Is the output given in the first post the actual format of the data as it is held on the file?

Would you provide a link to the actual data file.

The program reads from this file

Lucy      Mubanga    F   2562346  
Mark      Bwalya     M   2436586  
john      peter      M   7483289  
Lisa      Phiri      F   3674765  
Chitalu   Malama     F   4672762 
Naomi     Kanaka     M   6436728  
Malika    Chewe      F   4729208  
Raymod    Daka       M   3894782  
Lucy      Kalinga    F   5849535  
Jack      Kakwekwe   M   7548394 
Emmanucle Fuka       M   4325673  
Brian     Mwale      M   5327834  
Lisa      MWeka      F   1895865  
Prince    MWamba     M   4563982 
Fuka      Malambo    F   6273244  
peter     Chizyuka   M   3429174
Leo       Chili      M   482518 



then it compares for a match in this file




john      peter      M   7483289  20174893  2017 90 88 74 60 70 50 68
Lisa      McSon      F   3674765  20813672  2018 70 34 67 88 44 30 02
Chitalu   Malama     F   4672762  20146437  2014 59 50 88 45 34 70 99
Frank     Robarts    M   6546727  20016367  2001 68 45 34 34 67 23 78
Malika    Chili      F   4729208  20137346  2013 45 34 56 34 56 34 35
Leo       Chili      M   482518   20207346  2020 65 80 74 66 64 56 79




If a match is found then it outputs this


   

First Name : john
  Last Name : peter
     Gender : M
 Student ID : 7483289
Course Code : 20174893
       Year : 2017
The Applicants has the Following Grades
     Math  	               20
  English                      38
  English literature           54
  Civic Ed                     60
  Accounts                     58
  Science                      40
  Biology                      39
  Accounts                     40

  First Name : Lisa
  Last Name : McSon
     Gender : F
 Student ID : 3674765
Course Code : 20813672
       Year : 2018
The Applicants has the Following Grades
  Math                          70
  English                       34
  Pure Math                     67
  Civic Ed                      88
  History                       54
  Physics                       50
  Chemistry                     67
  Biology                       56
  

  First Name : Chitalu
  Last Name : Malama
     Gender : F
 Student ID : 4672762
Course Code : 20146437
       Year : 2014
The Applicants has the Following Grades
  Math                         59
  English                      50
  Sicence                      88
  Food and Nutrition           45
  Biology                      56
  Religious Education          60
  Geography                    55
  



  First Name : Malika
  Last Name : Chili
     Gender : F
 Student ID : 4729208
Course Code : 20137346
       Year : 2013
The Applicants has the Following Grades
  Math                         45
  English                      34
  Science                      56
  Art & Music                  34
  Biology                      56
  History                      49
  Civic Ed                     37

First Name : :Leo
  Last Name : Chili
     Gender : M
 Student ID : 4825218
Course Code : 20207346
       Year : 2013
The Applicants has the Following Grades
  Busniess Math                65
  Math                         80
  English                      74
  Science                      66
  Technology Studies           64
  I.T                          56
  History                      79
  Civic Ed                     87  


so now i need it to calculate or check if the students meet the criteria


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

using namespace std;
int main()
{
 struct person
{
  string firstname;
  string surname;
  string gender;
  int    id;
  int Course_Code;
  int  Year;
  string Subject_1;
  string Subject_2;
  string Subject_3;
   string Subject_4;
    string Subject_5;
     string Subject_6;
      string Subject_7;
       string Subject_8;
        string Subject_9;
};

system("cls");
  ifstream input;
  ifstream source;
  ofstream output;
  ofstream notfound;
  input.open("input.txt");
  source.open("source.txt");
  output.open("Output Processed file.txt");
  notfound.open("Not Found.txt");
  if(!input)
      {
      cout << "Error!" << endl;
      cout << "Unable to access the input.txt file required to run this process..." << endl;
      cout << "********************************************************************" << endl;
      cout << "The Process Was not Sucessful!! enter 0 Return to Previous Menu" << endl;
      }
if(source.fail())
    {
     cout << "Error!" << endl;
     cout << "Unable to access the source.txt file required to run this process" << endl;
     cout << "Its either it was Deleted or placed in a location inaccessable for this Program" << endl;
     cout << "Please check and re-run the process" << endl;
     cout << "" << endl;
     cout << "********************************************************************" << endl;
     cout << "The Process Was not Sucessful!! enter 0 Return to Previous Menu" << endl;

    }

  // foreach line of input....
  string input_line;
  while( getline(input,input_line) )
    {
    stringstream i_line(input_line);
    person input_person;
    i_line >> input_person.firstname
           >> input_person.surname
           >> input_person.gender
           >> input_person.id
           >> input_person.Course_Code
           >> input_person.Year
           >> input_person.Subject_1
           >> input_person.Subject_2
           >> input_person.Subject_3
           >> input_person.Subject_4
           >> input_person.Subject_5
           >> input_person.Subject_6;

    // search the source for a matching name
    source.clear();
    source.seekg(0);
    bool found = false;
    string source_line;
    while ( !found && getline(source,source_line) )
      {
      stringstream s_line(source_line);
      person source_person;
      s_line >> source_person.firstname
             >> source_person.surname
             >> source_person.gender
             >> source_person.id
             >>source_person.Course_Code
             >>source_person.Year;
      // found them!
      if ( source_person.id == input_person.id)
            {
            found = true;
            }
      }
      // and output...
      if ( !found )
            {
            notfound << "No NRC match for:" << source_line << endl;
            }
      else
            {
            cout << "NRC Match Found for:" << source_line<<  endl;
            output
        << std::setw(70) << "First Name : " << input_person.firstname << '\n'
        << std::setw(14) << "Last Name : " << input_person.surname << '\n'
        << std::setw(14) << "Gender : " << input_person.gender << '\n'
        << std::setw(14) << "Student ID : " << input_person.id << '\n'
        << std::setw(14) << "Course Code : " << input_person.Course_Code <<'\n'
        << std::setw(14) << "Year : " << input_person.Year << '\n'
        << std::setw(14) <<"The Applicants has the Following Grades" <<'\n'
        << std::setw(14) << input_person.Subject_1 << '\n'
        << std::setw(14) << input_person.Subject_2 << '\n'
        << std::setw(14) << input_person.Subject_3 << '\n'
        << std::setw(14) << input_person.Subject_4 << '\n'
        << std::setw(14) << input_person.Subject_5 << '\n'
        << "\n\n";

            }
    }
            input.close();
            source.close();
            output.close();
            notfound.close();
            cout << "" << endl;
            cout << "********************************************************************" << endl;
            cout << "The Process Was Sucessful!! enter 0 Return to Previous Menu" << endl;


}

/*this is were the am stuck..i need ideas of a code.. how its going to check if the student meets the criteria using the explained conditions in my question above 

*/



Last edited on
I ask again - what is the actual format of the input data file(s) as there now appears to be 2. The number of subjects/grades seems to be variable from the info provided in post #1. I don't see any code obtaining the actual mark for a subject. I'd suggest a container such as a vector to hold a struct of subject/marks - with eg a vector to hold the info per student. But to advise on good data design, we need to know the file layout(s).

A possible data based so far could be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
map<int subject_code, string subject> subjects;

struct Subject {
    int subject_code
    int mark;
};

struct Student {
    string first_name;
    string last_name;
    char gender;
    int id;
    int course_code;
    int year;
    vector<Subject> subjects;
};

vector<Student> students;

Last edited on
Thanks your continued response i've replied with all the in information in your other reply..please check through
Where are the subject names obtained from?

From

john      peter      M   7483289  20174893  2017 90 88 74 60 70 50 68


Presumably the last 7 numbers are grades - but for which subjects? as different students take different subjects.
https://quabr.com/61977080/hello-with-the-code-below-i-want-to-read-line-by-line-from-the-two-files-input-a

https://stackoverflow.com/questions/61971120/i-am-trying-to-read-from-the-input-file-then-compare-a-specific-part-of-a-string

http://www.cplusplus.com/forum/general/270707/

( On a lighter note while Student No. 6273244 has an unfortunate name outside Nigeria(?), our very own Kennedy gets a mention via No. 4729208 and again at No. 7483289 on the other site )

Last edited on
@seeplus yes students take different subjects and john pater's information was extracted from the file with the formate as below

Named input.txt




john      peter      M   7483289  20174893  2017 90 88 74 60 70 50 68
Lisa      McSon      F   3674765  20813672  2018 70 34 67 88 44 30 02
Chitalu   Malama     F   4672762  20146437  2014 59 50 88 45 34 70 99
Frank     Robarts    M   6546727  20016367  2001 68 45 34 34 67 23 78
Malika    Chili      F   4729208  20137346  2013 45 34 56 34 56 34 35
Leo       Chili      M   482518   20207346  2020 65 80 74 66 64 56 79



While this is the source.txt file format


Lucy      Mubanga    F   2562346  
Mark      Bwalya     M   2436586  
john      peter      M   7483289  
Lisa      Phiri      F   3674765  
Chitalu   Malama     F   4672762 
Naomi     Kanaka     M   6436728  
Malika    Chewe      F   4729208  
Raymod    Daka       M   3894782  
Lucy      Kalinga    F   5849535  
Jack      Kakwekwe   M   7548394 
Emmanucle Fuka       M   4325673  
Brian     Mwale      M   5327834  
Lisa      MWeka      F   1895865  
Prince    MWamba     M   4563982 
Fuka      Malambo    F   6273244  
peter     Chizyuka   M   3429174
Leo       Chili      M   482518 


Last edited on
@againtry
Yes earlier in my question i post my code of use you can run to see it works okay...
My problem is that i need the program to satisfy the criteria like i mentioned above please read through....
Thansks for your respose
For the first part, this will read the two files and display the info re the matching students. To calculate the final student grade, it is necessary to know to which subject the marks apply.

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
#include <set>
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>

struct Student {
	std::string first;
	std::string last;
	char init;
	unsigned long id;
	unsigned long course;
	unsigned long year;
	std::vector <unsigned long> marks;
};

int main()
{
	std::ifstream studs("students.txt");
	std::ifstream marks("marks.txt");

	if (!studs.is_open() || !marks.is_open())
		return (std::cout << "Cannot open input files\n"), 1;

	std::set<unsigned long> sids;

	unsigned long sid {};

	for (std::string item; studs >> item >> item >> item >> sid; )
		sids.insert(sid);

	std::vector<Student> students;

	for (std::string line; std::getline(marks, line); ) {
		std::istringstream iss(line);

		Student s;

		iss >> s.first >> s.last >> s.init >> s.id >> s.course >> s.year;

		if (sids.count(s.id) == 1) {
			unsigned long grade {};

			while (iss >> grade)
				s.marks.push_back(grade);

			students.push_back(s);
		}
	}

	for (const auto& s : students) {
		std::cout << s.first << "  " << s.last << "  " << s.init << "  " << s.id << "  " << s.course << "  " << s.year << '\n';

		for (const auto& m : s.marks)
			std::cout << m << "  ";

		std::cout << "\n\n";
	}
}


giving


john  peter  M  7483289  20174893  2017
90  88  74  60  70  50  68

Lisa  McSon  F  3674765  20813672  2018
70  34  67  88  44  30  2

Chitalu  Malama  F  4672762  20146437  2014
59  50  88  45  34  70  99

Malika  Chili  F  4729208  20137346  2013
45  34  56  34  56  34  35

Leo  Chili  M  482518  20207346  2020
65  80  74  66  64  56  79


Not formatted as needed, but the required output data.

From the data structure used, it's then fairly easy to obtain the required grade - once it is known to which subject the marks refer.
Thanks @seeplus
Then we assume its something like this


john      peter      M   7483289  20174893  2017 Math-90 Eng-88 Eng_lit-54 Civic_ED-74 Accts-60 Sic-70 Bio-50

Lisa      McSon      F   3674765  20813672  2018 Math-70 Eng-34 PMath-67 Civic_ED Hist-88 Phys-44 Chem-30 Bio-02

Chitalu   Malama     F   4672762  20146437  2014 Math-59 Eng-50 Sic-88 FnN-45 Bio-34 R.E-70 Geo-99

Frank     Robarts    M   6546727  20016367  2001 Math-68 Eng-45 AnM-34 Bio-34 Hist-67 Civic_ED-23

Malika    Chili      F   4729208  20137346  2013 Math-45 Eng-34 AnM-56 Bio-34 Hist-56 Civic_ED-34 35

Leo       Chili      M   482518   20207346  2020 Bus_Math-65 Eng-80 Sic-74 Tech-66 I.T-64 Hist-56 Civic_ED-79

As abbreviated
Eng_lit = English Literature
Civic_ED = Civic Education
Accts = Accounts
Sic = Sicence
Bio = Biology
PMath = Pure Math
Phys = physics
Che = Chemistry
FnN = Food And Nurition
R.E = Religious Education
AnM = Art and Music
Hist = History
Tech = Technology studies




For my problem its important it satisfies the criteria at the end of the execution

Given the marks file


john      peter      M   7483289  20174893  2017 Math-90 Eng-88 Eng_lit-54 Civic_ED-74 Accts-60 Sic-70 Bio-50
Lisa      McSon      F   3674765  20813672  2018 Math-70 Eng-34 PMath-67 Civic_ED Hist-88 Phys-44 Chem-30 Bio-02
Chitalu   Malama     F   4672762  20146437  2014 Math-59 Eng-50 Sic-88 FnN-45 Bio-34 R.E-70 Geo-99
Frank     Robarts    M   6546727  20016367  2001 Math-68 Eng-45 AnM-34 Bio-34 Hist-67 Civic_ED-23
Malika    Chili      F   4729208  20137346  2013 Math-45 Eng-34 AnM-56 Bio-34 Hist-56 Civic_ED-34
Leo       Chili      M   482518   20207346  2020 Bus_Math-65 Eng-80 Sic-74 Tech-66 I.T-64 Hist-56 Civic_ED-79


Consider:

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
#include <set>
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <iomanip>

struct Student {
	std::string first;
	std::string last;
	char init {};
	unsigned long id {};
	unsigned long course {};
	unsigned long year {};
	std::map <std::string, unsigned long> marks;
};

int main()
{
	 const std::map<std::string, std::string> subjects {{"Eng_lit", "English Literature"},
												{"Eng", "English"},
												{"Civic_ED" , "Civic Education"},
												{"Accts" , "Accounts"},
												{"Sic" , "Science"},
												{"Bio" , "Biology"},
												{"PMath" , "Pure Math"},
												{"Math", "Mathematics"},
												{"Bus_Math", "Business Mathematics"},
												{"Phys" , "Physics"},
												{"Chem" , "Chemistry"},
												{"FnN" , "Food And Nutrition"},
												{"R.E" , "Religious Education"},
												{"AnM" , "Art and Music"},
												{"Geo", "Geography"},
												{"Hist" , "History"},
												{"I.T", "Information Technology"},
												{"Tech" , "Technology studies"}};

	std::ifstream studs("students.txt");
	std::ifstream marks("marks.txt");

	if (!studs.is_open() || !marks.is_open())
		return (std::cout << "Cannot open input files\n"), 1;

	std::set<unsigned long> sids;

	unsigned long sid {};

	for (std::string item; studs >> item >> item >> item >> sid; )
		sids.insert(sid);

	std::vector<Student> students;

	for (std::string line; std::getline(marks, line); ) {
		std::istringstream iss(line);
		Student s;

		iss >> s.first >> s.last >> s.init >> s.id >> s.course >> s.year;

		if (sids.count(s.id) == 1) {
			for (std::string submark; iss >> submark; ) {
				const auto sep {submark.find('-')};

				if (sep != std::string::npos) {
					const auto ccode {submark.substr(0, sep)};

					if (subjects.count(ccode) == 1)
						s.marks.emplace(ccode, std::stoul(submark.substr(sep + 1)));
					else
						std::cout << "Subject " << ccode << " Not found\n";
				}
			}

			students.push_back(s);
		}
	}

	for (const auto& s : students) {
		std::cout << s.first << "  " << s.last << "  " << s.init << "  " << s.id << "  " << s.course << "  " << s.year << "\n\n";

		bool OK {s.marks.size() >= 6};

		size_t mes {};
		size_t other {};

		// Work out if met criteria

		// 1) and 2)
		// Others to add

		for (const auto& m : s.marks) {
			std::cout << std::left << std::setw(24) << subjects.find(m.first)->second << m.second << "\n";

			if (m.first == "Math" || m.first == "Eng" || m.first == "Sic")
				mes +=( m.second > 50);
			else
				other += (m.second > 50);
		}

		if (mes == 3 && other == 3)
			std::cout << "\nCriteria met!";
		else
			std::cout << "\nCriteria not met";

		std::cout << "\n\n\n";
	}
}


which displays:


john  peter  M  7483289  20174893  2017

Accounts                60
Biology                 50
Civic Education         74
English                 88
English Literature      54
Mathematics             90
Science                 70

Criteria met!


Lisa  McSon  F  3674765  20813672  2018

Biology                 2
Chemistry               30
English                 34
History                 88
Mathematics             70
Pure Math               67
Physics                 44

Criteria not met


Chitalu  Malama  F  4672762  20146437  2014

Biology                 34
English                 50
Food And Nutrition      45
Geography               99
Mathematics             59
Religious Education     70
Science                 88

Criteria not met


Malika  Chili  F  4729208  20137346  2013

Art and Music           56
Biology                 34
Civic Education         34
English                 34
History                 56
Mathematics             45

Criteria not met


Leo  Chili  M  482518  20207346  2020

Business Mathematics    65
Civic Education         79
English                 80
History                 56
Information Technology  64
Science                 74
Technology studies      66

Criteria not met


At the moment it only uses criteria 1) and 2). Other criteria should be fairly easy to add now.
V2. Re-factored code into functions. If have time will add further criteria checking.

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
#include <set>
#include <fstream>
#include <string>
#include <iostream>
#include <sstream>
#include <vector>
#include <map>
#include <iomanip>
#include <utility>

const std::map<std::string, std::string> subjects {{"Eng_lit", "English Literature"},
										   {"Eng", "English"},
										   {"Civic_ED" , "Civic Education"},
										   {"Accts" , "Accounts"},
										   {"Sic" , "Science"},
										   {"Bio" , "Biology"},
										   {"PMath" , "Pure Math"},
										   {"Math", "Mathematics"},
										   {"Bus_Math", "Business Mathematics"},
										   {"Phys" , "Physics"},
										   {"Chem" , "Chemistry"},
										   {"FnN" , "Food And Nutrition"},
										   {"R.E" , "Religious Education"},
										   {"AnM" , "Art and Music"},
										   {"Geo", "Geography"},
										   {"Hist" , "History"},
										   {"I.T", "Information Technology"},
										   {"Tech" , "Technology studies"}};

using SubMark = std::pair<const std::string, unsigned long>;
using Marks = std::map<std::string, unsigned long>;

struct Student {
	std::string first;
	std::string last;
	char init {};
	unsigned long id {};
	unsigned long course {};
	unsigned long year {};
	Marks marks;
};

std::istream& operator>>(std::istream& is, Student& s)
{
	std::string line;
	s.marks.clear();

	if (std::getline(is, line)) {
		std::istringstream iss(line);

		iss >> s.first >> s.last >> s.init >> s.id >> s.course >> s.year;

		for (std::string submark; iss >> submark; ) {
			const auto sep {submark.find('-')};

			if (sep != std::string::npos) {
				const auto ccode {submark.substr(0, sep)};

				if (subjects.count(ccode) == 1)
					s.marks.emplace(ccode, std::stoul(submark.substr(sep + 1)));
				else
					std::cout << "Subject " << ccode << " Not found\n";
			}
		}
	}

	return is;
}

std::ostream& operator<<(std::ostream& os, const Student& s)
{
	return os << s.first << "  " << s.last << "  " << s.init << "  " << s.id << "  " << s.course << "  " << s.year << "\n";
}

std::ostream& operator<<(std::ostream& os, const SubMark& m)
{
	return os << std::left << std::setw(24) << subjects.find(m.first)->second << m.second;
}

bool process(const Marks& marks)
{
	unsigned mes {}, other {};
	bool OK {marks.size() >= 6};

	for (const auto& m : marks) {
		std::cout << m << '\n';

		if (m.first == "Math" || m.first == "Eng" || m.first == "Sic")
			mes += (m.second > 50);
		else
			other += (m.second > 50);
	}

	return OK && mes == 3 && other >= 3;
}

int main()
{
	std::ifstream studs("students.txt");
	std::ifstream marks("marks.txt");

	if (!studs.is_open() || !marks.is_open())
		return (std::cout << "Cannot open input files\n"), 1;

	std::set<unsigned long> sids;

	unsigned long sid {};

	for (std::string item; studs >> item >> item >> item >> sid; )
		sids.insert(sid);

	for (Student s; marks >> s; )
		if (sids.count(s.id) == 1) {
			std::cout << s << '\n';

			if (process(s.marks))
				std::cout << "\nCriteria met!";
			else
				std::cout << "\nCriteria not met";

			std::cout << "\n\n\n";
		}
}

Last edited on
Thanks @seeplus
Can you compile as C++17?

PS

if is Science is not part of the subjects
Physics and Chemistry are considered.


What is meant by 'considered'?
Last edited on
Updated process() function that deals with Pure Maths & English Lit [Note not fully tested]

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
bool process(const Marks& marks)
{
	struct Alter {
		std::string main;
		std::string alt;
	};

	static const Alter alsubs[] {{"Math", "pMath"}, {"Eng", "Eng_lit"}};
	static const std::string mustHave[] {"Math", "Eng", "Sic"};

	const unsigned pass {51};

	unsigned mes {}, other {};

	for (const auto& m : marks) {
		std::cout << m << '\n';

		const auto fnd {std::find_if(std::begin(alsubs), std::end(alsubs), [&m](const auto& sub) {return sub.main == m.first; })};

		if (m.second < pass && fnd != std::end(alsubs)) {
			const auto ret {marks.find(fnd->alt)};

			if (ret != marks.end())
				mes += ret->second > m.second;
		}

		const auto fndm {std::find(std::begin(mustHave), std::end(mustHave), m.first)};

		if (fndm != std::end(mustHave))
			mes += m.second >= pass;
		else
			other += m.second >= pass;
	}

	return mes == 3 && other >= 3;
}

Last edited on
Topic archived. No new replies allowed.