Read Text file-Store info w/ Vector

So in my C++ course we were given instructions to:
Write a program that reads a text file that has a student’s
full name (on its own line) and several test scores after it. 
Make sure there is NO WHITE SPACE after the last  number.  The number of scores is unknown, and may  change since the student’s teacher will open the file and 
type in new grades whenever they come up.  Your  program will always open the file, read the student name  and then store the grades in a vector.  The program will 
then compute the test average  ­ throw out the lowest  score (only if there are at least two scores).


Currently I am able to open the text file and out put exactly what it says as a string, but I am having problems with figuring out how to store data / make variables. So how would I be able to store each grade (double variables) within the vectors then conduct the math? I'm sorry for asking for so much but my teacher didn't teach this that well and her notes don't cover the material well either!

My code so far:
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
#include <iostream>
#include <vector>
#include <fstream>
#include <string>

using namespace std;

void fill (ifstream &fin, vector <string> &name);

template <typename pringle>
void display(vector <pringle> name)
{
for (int i=0; i<name.size(); i++)
cout<<name[i]<<" "<<;
cout<<endl;

}

int main ()
{
vector <string> name;
double average, removed;
ifstream fin;
fin.open("Tot Katzen.txt");
fill(fin,name);
display(name);
return 0;
}


void fill(ifstream &fin, vector <string> &name)
{
while (!fin.eof())
{
name.resize(name.size()+1);
getline(fin, name[name.size()-1]);
fin.ignore(80, '\n');
}

}



My text file is named "Tot Katzen.txt" and the contents in it is
1
2
Com Truise
34.5 40 50 80



Thank you for anyone can lead me in the right direction!
"fill" is a terrible name for a function to read a file.

Don't loop on EOF.

The assignment is unclear about the text file format.

May there be more than one student listed in the file?
Must all the test scores be on the same line?

(The only thing it is clear about is the whitespace stuff, which is actually an insignificant issue when reading the file.)

I don't understand your use of language. (The best I can get out of your filename is something about dead cats?)

I will assume multiple students may be listed in the file, since it is a worst-case kind of scenario, and the resulting thought will work even if there is only one student listed.


So, here is an example file that covers all possibilities:

Com Truise
34.5 40 50 80
Livien Veigh
74.3
88.5
90
100


Q. How would you store this information?
A. For each student, you need a name and a list of grades, right?

For a name, you need a string. (Just one!)
For a list of grades, you need a vector of doubles.

To hold a student, you need a struct/class.

1
2
3
4
5
struct Student
{
  string         name;
  vector <double> grades;
};

If your file contains only one student, you'll want something like:

1
2
3
4
int main()
{
  Student student;
  ...

If your file contains multiple students, you'll want a vector of them, right?

1
2
3
4
int main()
{
  vector <Student> students;
  ...


Either way, you also need a function to read a single student from file.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
Student read_student( ifstream& file )
{
  Student student;

  // First, read the student's name
  // Then you need a loop to read grades, until the loop fails.
  while (file >> grade)
  {
    // Once you have a grade, add it to the list of grades
  }

  // At this point, it makes a difference whether you have hit EOF or not.
  // If you have, then leave the file in its error state. Otherwise you must 
  // clear it for the next student (or whatever other kind of thing may be
  // in the file).
  if (!file.eof()) file.clear();

  return student;
}

You can use this easily enough:

1
2
3
4
5
6
7
8
int main()
{
  Student student;

  ifstream fin("Tot Katzen.txt");
  student = read_student(fin);

  ...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()
{
  vector <Student> students;

  ifstream fin("Tot Katzen.txt");
  while (true)
  {
    // Try to read a student from the file
    Student student = read_student(fin);

    // Only if a student could not be read, quit reading from file
    if (!fin) break;

    // Otherwise, add the student just read to our list of students
    students.push_back(student);
  }

  ...


Now you have to deal with deleting the lowest score and computing the average of the/each student's grades.

Good luck!
Sorry the text file title was just a joke among class mates haha.

There should only be one student but the code should operate no matter where the grades are placed at whether they are on the same line with the name or under it. I'm sorry for my Teacher's vague instructions, the whole class is struggling with it as well.

What is the "Student" variable you are referring to in your code sections? Where/how did you call it?

Also are teacher has yet to teach us the struct class.

Also how are you declaring
vector <Student> students;

I apologize with my lack of ability to work through this code, but as I said earlier my teacher hasn't supplied enough information to work out this program properly and I been searching the web for something similar but I can't seem to find any other source code that can be near the relevancy to what I need or know.



Also in that while loop where is "grade" declared at?
Last edited on
My response is pretty explicit about the "Student" type.

But since you have not studied structs/classes, you don't need it.

You just need the things I already indicated.
Sorry I just figured out about what you mean't about the "Student" haha.

I sort of understand the use of it now, but in the while loop what am I suppose to compare the file with when you put in "grade"and while it loops where is the grade suppose to be added to?

I hate to keep asking these broad and basic questions but this unit truly has me confused if it helps here is a link to the notes that we received with all the resources that were given to us during this chapter (its a small pdf file)

http://mhscs.net/Dierker/cpp/vectors/vectprac.pdf

At the end it has our assignment.
Topic archived. No new replies allowed.