Pointers and vector output.

Hey guys, beginner here making his first post so apologies if I make this more confusing than it needs to be.

So here's the problem, I was given this Student class. A user will insert a name followed by a double representing a GPA. The user will enter these in pairs followed by the word "STOP". With each name and GPA, I have to create a Student pointer and insert it into the "students" vector. I must print all the GPAs in the vector in reverse order.

I've got this far, but the output comes out as:
3.9
Kenny
3.65
George
2.8
Bob

When it should be:
3.9
3.65
2.8

How should I go about removing the names from the output?

Thanks!

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
#pragma once
#include <iostream>
#include <string>
#include <vector>

using namespace std;

class Student
{
private:
	string name;
	double gpa;

public:
	Student(string name, double newGPA);
	string getName();
	void setName(string newName);
	double getGPA();


	~Student();
};

Student::Student(string studentName, double newGPA)
{
	name = studentName;
	gpa = newGPA;
}

string Student::getName()
{
	return name;
}

void Student::setName(string newName)
{
	name = newName;
}

double Student::getGPA()
{
	return gpa;
}

Student::~Student()
{
}

//
// You may insert a function here

int main()
{
	vector<Student*> students;
	
	double gpa = 0;
	string line = "";
	string name = "";
	
	while(line != "STOP")
	{
	    cin >> name;
	    if (name == "STOP")
	    break;
	    Student * student = new Student(name, gpa);
	    students.push_back(student);
	}
	for (int i = students.size()-1; i >= 0; i--)
	{
	    cout << students[i]->getName()<< endl;
	}
	return 0;
}
Last edited on
You have forgotten reading into the gpa. Thus, if you input a student's name followed by the GPA, then your GPA input is pushed back as anoter Studen's name.

Here my suggestion:
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
...
int main()
{
	vector<Student*> students;
	
	double gpa = 0;
	// string line;
	string name;
	
	while (true)
        {
            cin >> name;
            
            if (name == "STOP") { break; }
            
            cin >> gpa;
           // If reading to gpa failed.
            if ( cin.fail() ) { break; }  // or handle this error
            
	    students.push_back( new Student( name, gpa) );
	}
	
	
	for (int i = students.size()-1; i >= 0; i--)
	{
	    cout << students[i]->getName()<< endl;
	}
	return 0;
}

You needn't initilalize string variables, because they will (by its default constructor) automatically initialized to "".
Last edited on
Oh i have overlooked that you wish to show the GPA values reversely, but I guess it's not a problem for you fixing the code to this behavior?
Thank you so much! That was really helpful. I did manage to display the GPA values reversely, yes.

Much appreciated!
Topic archived. No new replies allowed.