Please help!!!! multiple lines of input and save data

Hey guys, For my assignment I have to do this:

"Write a program to do examination report generation for the Secondary 6 Science class.

The scores are stored in a file, one student per line, with the following format:
Peter Pan 27 Math 75 85 80 53
Lee Ka Yan 3 Bio 53 63 75
...
The fi le contains the following fields: Name, id, group, and marks, and the fields are separated by a single tab. The marks given are integers between 0 and 100. A Math student takes 4 subjects while a Bio student takes 3 subjects. The passing mark is 50.
A student has to pass at least 2 subjects, and with an overall average mark of 60 or above to pass the year. For each student, generate a fi le with fi lename Report-id.dat, where id is the 3-digit id of the student, padded in front by 0. For example, the fi lename for Peter Pan is Report-027.dat, and the filename for Lee Ka Yan is Report-003.dat.
The Report fi le has the following content:

Name: Peter Pan
Student id: 27
Group: Math
Marks: 75 85 80 53
Average: 73.25
Status: Pass

Sample run of program:

Enter the datafile to process:
studentscores.txt
A number of Report-XXX.dat fi les should be created as program output."

So my 2 questions are:
1. how do you separate the inputs at a new line?
2. How do you save the data into a .dat file? So the name of the .dat file is Report-id.dat (eg Report-027.dat for Peter Pan) and then make the file look like this:
Name: Peter Pan
Student id: 27
Group: Math
Marks: 75 85 80 53
Average: 73.25

Sorry for asking so much but this is really important and I have no idea how to do this at all! Please help!!! Many thanks!!
Last edited on
You have a number challenges
start small
write a program to open a file (ifstream)
read the first line (getline)
output that to screen (cout)

1. then your ready for the next challenge
take the 1st line and determine if the next char is a Letter, space, number or newline.
If you can output it to the screen you can output it to a file later.

2. You output a .dat the same way you do a .txt
look up ofstream

once you get that working, read in the group and remaining numbers
count them as you read them in
add them together
divide totalnumber/count for average

once you get all that working, you will code in a string for the filename
modify the string, open the file and write to it as needed, be sure to close it when done.

If you take each problem as a small pc of code, and work on that soon you'll be done, or have something to post we can look at with you.


I'm kinda stuck on determining the next char....anyway so far this is what I got:
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
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

int main(void)
{
	ifstream myfile;
	string (x);
	string (name);
	char filename[150];

	cout << "Enter report file name:" << endl;
	cin.getline(filename, 150); 

	myfile.open(filename);

	if (myfile.is_open()){
		
		while (myfile >> x){
			
			cout << x << '\t';
			
		}
	}else cout << "Error" << endl;

	system("pause");
	return 0;
}
Last edited on
@ johnnyboy710 The suggestion from SamuelAdams was a reasonable one.
Notice he said this: read the first line (getline)
However, the code which you have posted is reading individual words, not an entire line. You need to address that problem before moving on.

The next part would be to process that line of data, which can be regarded as a task in its own right. Hence I'd recommend you use a separate function, perhaps something like void processStudent(const std::string data);

The description of the project says each field of the data line is separated by a single tab. There are lots of ways you could get the individual fields from the line. My suggestion would be to use a stringstream.
Out of interest... any progress ??
i got a problem similar :(
Topic archived. No new replies allowed.