From IOstream into a vector

Im trying to write a program which reads information (a student number and the GPA of the student) from a file, displays the information. also Compares the GPA of the student to determine the highest gpa, then displays the student number and the gpa of that student

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

using::std::copy;
using namespace std;

struct Student
{
  int ID;
  double GPA;
 
};

int main(int argc, char* argv[])

{
	vector<double>fileToVector;
	ifstream readFile;
	Student student;
	
	readFile.open("file_name.txt");
	if (readFile.fail()){	cout << "File open failed.\n";	exit(1);}
		
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	readFile.precision();
	
	while(readFile >> student.ID >> student.GPA)
		cout << student.ID << " " << setw(2) << student.GPA << endl;
		

	readFile.close();
	//writeFile.close();
	return 0;
}

Thus far
Last edited on
So what problem are you having?

I suspect you want a vector of Students, not a vector of doubles.

PLEASE USE CODE TAGS (the <> formatting button) when posting code.
It makes it easier to read your code and also easier to respond to your post.
http://www.cplusplus.com/articles/jEywvCM9/
Hint: You can edit your post, highlight your code and press the <> formatting button.
Yes i want to assign the information from the file
which is student number followed by a space and then the gpa of the student,
i want to assign the everyline of the file to a student object, then use the find the highest gpa and display it with the student number.
@AbstractionAnon
1
2
3
4
5
//  Replace line 20 with: 
    vector<Student> students;
//  Replace line 32 with: 
    students.push_back (student);
//  line 34:  find highest GPA in the vector 

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

using::std::copy;
using namespace std;

struct Student
{
  int ID;
  double GPA;
 };


int main(int argc, char* argv[])

{
	vector<Student>students;
	int k;
	ifstream readFile;
	
	
	//ofstream writeFile;

	readFile.open("file_name.txt");
	if (readFile.fail()){	cout << "File open failed.\n";	exit(1);}
		
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	readFile.precision();

	Student student;
	
	
	while (readFile >> student.ID >> student.GPA)
	 {
		 students.push_back (student);
		 cout << student.ID << " " << setprecision(1) << student.GPA << endl;

		 	
	}
	

	readFile.close();
	//writeFile.close
	cin >> k;
	return 0;
}



i also have to display the contents of the file, but im not sure have to access the vector , and to check if the contents of the file is actually in the vector.
A std::vector is analogous to a managed array. Simply reference the elements.
1
2
3
//  After line 44, display the contents of the array
    for (int i=0;i<students.size(); i++)
      cout << students[i].ID << " " << students[i].GPA << endl;


I'll leave it to you to find the highest GPA in the vector.

thank you :
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
#include <iostream>
#include <fstream>
#include <iomanip>
#include <vector>
#include <string>

using::std::copy;
using namespace std;

struct Student
{
  int ID;
  double GPA;
 };


int main(int argc, char* argv[])

{
	vector<Student>students;
	int k;
	ifstream readFile;
	std::string student2;
	
	

	readFile.open("file_name.txt");
	if (readFile.fail()){	cout << "File open failed.\n";	exit(1);}
		
	cout.setf(ios::fixed);
	cout.setf(ios::showpoint);
	readFile.precision();

	Student student;
	
	while (readFile >> student.ID >> student.GPA)
	 {
		  
		  students.push_back (student);
		 	
	}
	 for (int i=0;i<students.size(); i++)
      cout << students[i].ID << " " << setprecision(1)  << students[i].GPA << endl;
	 
	 double temp=students[0].GPA;
	 int gpa;
	 for(int j=0 ; j < students.size(); j++)
	 {
		 if(students[j].GPA>temp)
		 {
			temp=students[j].GPA;
			gpa=j;

		}

	 }

	 cout <<"Student " << students[gpa].ID << " has the highest GPA of " << temp;
	readFile.close();
	
	cin >> k;
	return 0;
}



i got it , so i thought id share :D, thank you again AbstractionAnon
Almost. There is a slight problem though.

line 46: gpa is uninitialized. If students[0] has the highest GPA, lines 51-52 never execute, leaving gpa uninitialized when you reach line 58.
it works well, i did change that, before i posted this :D
Topic archived. No new replies allowed.