Vectors and Loops

I am getting an error saying expected primary expression before void. This is my assignment. Hopefully I am doing this right but I am having trouble with this error. Any help would be appreciated
Write a main function that does the followings:
--- Create an empty vector of Student class (see code above).
--- Add 4 Student objects to the vector using the push_back() method.
--- Use a loop to show GPAs of 4 objects.
--- Insert a new Student object in the middle of the vector.
--- Use a loop to show GPAs of 5 objects.


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
66
67
68
69
70
71
72
73
#include <iostream>
#include <string>
#include <vector>
using namespace std; 

 class Student
{
     protected:
         int ID;
         float mathScore;
         float chemScore;
         float engScore;
     public:
         Student (int = 0, float = 0.0f, float = 0.0f, float = 0.0f);
         void getGPA();
};

Student::Student (int id, float ms, float cs, float es)
{
     ID = id;
     mathScore = ms;
     chemScore = cs;
     engScore = es;
}

void Student::getGPA ()
{
     float gpa = (mathScore + chemScore + engScore)/3.0;
     cout << "ID = " << ID << " PGA = " << gpa << endl;
}
         
int main()

{
   vector<Student> s;                      
   s.push_back(Student(999));       
   s.push_back(Student(80));   
   s.push_back(Student(55.6));
   s.push_back(Student(70));
   
   vector<Student>::iterator it;         
   cout << endl;
   for (it = s.begin(); it != s.end(); ++it) 
{
  cout << *void.getGPA() << endl;  
 //cout << void->getGPA()   << endl;      
 }
 {
  cout << *void.getGPA() << endl;  
 //cout << void->getGPA()   << endl;      
 }
 {
 cout << *void.getGPA () << endl;  
 //cout << void->getGPA ()   << endl;    
 }
  {
 cout << *void.getGPA () << endl;  
 //cout << void->getGPA ()   << endl;    
 }
cout << endl;
it = s.begin();     
*it = Student(44.9);        
cout << endl;
for (it = s.begin(); it != s.end(); ++it) 
{
  cout << *void.getGPA() << endl;  
 //cout << void->getGPA()   << endl;      
 }
 s.erase((s.begin() + 1));    
 cout << endl;
 for (it = s.begin(); it != s.end(); ++it) 
 return 0;
}
first lets take care of the compiler errors:

cout << *void.getGPA() << endl;

I can't even figure out what you are doing here. getGPA() belongs to the Student class. Since you have a vector of them, and an iterator, you really want to reference using the iterator. You also can't pass cout anything from a void function. it->getGPA(); Will do what you want, which is print the student's GPA. You are also using a for loop to iterate through the vector, so you don't need to print the GPA 4 times per iteration.

*it = Student(44.9);

This is not inserting a new student into the vector, rather replacing an existing one. You should use s.insert(it,Student(44.9))

I suggest you just go error-by-error and try to fix it, then post again if you get stuck
thank you so much I ran the code and it compiled successfully 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
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
#include <iostream>
#include <string>
#include <vector>
using namespace std; 

 class Student
{
     protected:
         int ID;
         float mathScore;
         float chemScore;
         float engScore;
     public:
         Student (int = 0, float = 0.0f, float = 0.0f, float = 0.0f);
         void getGPA();
};

Student::Student (int id, float ms, float cs, float es)
{
     ID = id;
     mathScore = ms;
     chemScore = cs;
     engScore = es;
}

void Student::getGPA ()
{
     float gpa = (mathScore + chemScore + engScore)/3.0;
     cout << "ID = " << ID << " PGA = " << gpa << endl;
}
         
int main()

{
   vector<Student> s;                      
   s.push_back(Student(999));       
   s.push_back(Student(80));   
   s.push_back(Student(55.6));
   s.push_back(Student(70));
   
   vector<Student>::iterator it;         
   cout << endl;
   for (it = s.begin(); it != s.end(); ++it) 
{ 
 //cout << it->getGPA()   << endl;      
 }
cout << endl;
it = s.begin();     
s.insert(it,Student(44.9));        
cout << endl;
for (it = s.begin(); it != s.end(); ++it) 
{ 
 //cout << it->getGPA()   << endl;      
 }
 s.erase((s.begin() + 1));    
 cout << endl;
 for (it = s.begin(); it != s.end(); ++it) 
 return 0;
}
Topic archived. No new replies allowed.