Need help finishing code (Read from 1 file and output to second)

Write a program that will calculate a student’s year marks for all his subjects. The program must read the subject code, the assignment marks for two assignments and the percentage that each assignment contributes towards the year mark, for each subject that the student is registered for.
Create an input file called assignments.dat that contains the following information for a specific student:
COS1511
30
66
70
49
COS1512
25
76
75
67
COS1521
10
58
90
62
COS1501
50
62
50
57
INF1501
40
82
60
78
INF1511
20
24
80
55
The first field in each line represents the subject code, followed by the percentage that assignment 1 contributes towards the year mark and the student’s mark (a percentage) for assignment 1. Then follow the percentage that assignment 2 contributes towards the year mark and the student’s mark for assignment 2. You should not count the number of entries in the file to determine how many times the file must be read.
Your program must read the file line by line, calculate the student’s year mark for the subject as a percentage and write the subject code and the year mark to an output file yearmark.dat. If your program works correctly, the output file will look as follows:
COS1511
54.10%
COS1512
69.25%
COS1521
61.60%
COS1501
59.50%
INF1501
79.60%
INF1511
48.80%

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
#include<iostream>
 #include<fstream>
 #include<string>
 #include<numeric>
 using namespace std;

 int main ()
 {

        string before {"30,66,70,49 ; 25,76,75,67 ; 10,58,90,62 ; 50,62,50,57 ; 40,82,60,78 ; 20,24,80,55"};
        string after {"(30+66+70+49)/4 ; (25+76+75+67)/4 ; (10+58+90+62)/4 ; (50+62+50+57)/4 ; (40+82+60+78)/4 ; (20+24+80+55)/4"};
        unsigned char tbl[256] {};
        iota(tbl, tbl + 256, static_cast<unsigned char>(0));

    cout << "The Input File is : assignments.dat" << endl;
     ifstream input_file("assignments.dat");
    cout << "The Output File is : yearmark.txt" << endl;
     ofstream output_file("yearmark.txt");


        if (!input_file || !output_file)
		return (cerr << "Could not open files\n"), 1;
			for (char ch {}; input_file.get(ch); output_file.put(tbl[ch]));

 }
What's the posted code got to do with problem? Why tbl? The after calculations are not correct as they are not 4 scores to average, but 2 scores with a % contribution for each score.
ye ok , so I read the question wrong , I just noticed as soon as I posted it , but the table is there cause I thought since the subjects names and the scores are kinda in a "table" form with tab's.

so if i were to calculate the average of the string + the weight % it counts , how would I do that?
Possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>

int main()
{
	std::ifstream ifs("assignments.dat");
	std::ofstream ofs("yearmark.dat");

	if (!ifs || !ofs)
		return ("Cannot open files\n"), 1;

	for (std::string sub; ifs >> sub; ) {
		double per1 {}, per2 {}, mrk1 {}, mrk2 {};

		if (ifs >> per1 >> mrk1 >> per2 >> mrk2)
			ofs << std::fixed << std::setprecision(2) << sub << '\n' << (mrk1 * per1 + mrk2 * per2) / 100.0 << "%\n";
	}
}

thank you seeplus, if I may just ask, I understand what you wrote me but like can you explain what you did? like how you knew what to write where... I know the question seems vague but if you understand what I mean?
First, read the question and make sure you understand it and note all the relevant points. Then a program design - input, output, calculations etc.

Once you're got the design (fairly simple in this case), you move on to the code.

L8 - 12 open the files and check that they're OK to use. If a program uses files, similar code is usually somewhere near the beginning of code.

Then you need to read the data from the file and process each 'record'. A record is a subject followed by 4 numbers. The end of file is when a read doesn't succeed.

So read the subject and if OK continue - otherwise end of file and exit.

Then read the 4 numbers. This could have been combined with reading the subject, but I did it separately so that I could define the variables within the loop body to keep the scope to a minimum.

Once the numbers are read OK (test in case there's a format issue with the file data), the required number is simply (mark1 * percent1 + mark2 * percent2) / 100 to get the weighted average.

Then output to the file in the required format.

Simplees!
Topic archived. No new replies allowed.