reading in file and displaying fields

So i started off on the wrong foot with this assignment, i started by using a set ammount of students, but i did it wrong cause i won't know how many students there will actually be.

the project is going to open a txt file that contains information such as:

3 6 600
1479 98 90 78 89 90 99
1785 79 88 89 90 93 75
1396 88 85 98 87 92 76

the 3 stands for number of student, 6 for how many graded tests and 600 for over all points.

(the file will be different from what the instructor will use, and will have more or less students then example file)

my question is how do i get my program to read in the data for each student without having a set number of students. here is what i have so far and reading the file works, but this is where i am stuck at, please help..

#include <iostream>
#include <fstream>
#include <iomanip>

using namespace std;

#define MAX_NAME 32


int main()
{
char fileName[MAX_NAME];

cout << "Enter a file name: ";
cin >> fileName;

ifstream in( fileName, ios::in);

if ( in.fail() )
{
cout << "Error opening file: " << fileName << endl;

return 0;
}

int numberStudent;
int grade;
int max;

in >> numberStudent >> grade >> max;


any suggestions are helpful,

the main goal is to read in the text file
show the student id and overall grade percentage and letter grade

example:

Student ID Percentage Grade
---------- ---------- -----
1479 89% B


i can do the display i understand that, but manipulating the data into seperate students, note: i cannot use arrays on this assignment, thats how i did it wrong the first time through.
my question is how do i get my program to read in the data for each student without having a set number of students.

Try a vector. You can use the push_back member function to add elements to it, so you don't need to know how many students you'll have.
The index number could be the student number, and each value is the score. Maybe another vector for max, too.

Hope that helps. It IS pretty late >.>
new vector?

Well you have just read in the number of students, the number of grades and the max score.

So now you could make a loop to read in each student and inside that another loop to read in their grades:

Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

// Once for each student
for(int s = 0; s < numberStudent; ++s)
{
	// Read in a student

	int id;
	int score[grade];
	int percent;

	// read in id here

	// once for each grade
	for(int g = 0; g < grade; ++g)
	{
		// read in each score
	}
	
	// calculate percent and grade
	// then output id , percent and grade
}



Last edited on
Topic archived. No new replies allowed.