Garbage output with class declaration and Vectors

Hello! I am having two issues with a program I am working on...please help!
First problem should be easy but I can't figure it out. Whenever I declare an instance of my 'student' class, I get output that appears to be an address...but I have no couts anywhere! How is that possible??


1
2
3
4
5
6
7
int main(int argc, char *argv[])
{
       
    float janeDoeTests[] = { 85, 89.5, 90, 75, 56, 79, 100, 45, 67, 88  };

    
    Student student1(1, "Jane Doe", janeDoeTests);


The constructor passes an ID, name and array. The Student class has a pointer to the array.

So when I just run these two lines of code--an address is outputted..???

PROBLEM #2

Assuming I was able to declare my classes, I want to make a vector of classes. But when I am trying to display the vector, I cant seem to access what the pointer data member is pointing to...it's the displayTests function...am I not calling it correctly?
1
2
3
4
5
6
7
8
9
10
11
12
void displayVector(vector<Student> vect)
{
    for(int count = 0; count < vect.size(); count++)
    {
         cout << "Student ID: " << vect[count].getID() << endl;
         cout << "Student Name: " << vect[count].getName() << endl;
         cout << "Student Average: " << vect[count].getAverage() << endl;
         cout << "Test Scores: " << vect[count].displayTests() << endl;        
         cout << endl;
    }
        
}


please help!!
Could you show the code for Student?
absolutely

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Student
{
      public:
             int studentID;
             string studentName;
             float studentAvg;
             float * testScores;
              
      public:
             Student();
             Student(int, string, float *);
             Student(const Student&);
             ~Student();
             int getID(){ return studentID; };
             string getName() { return studentName; };
             void displayTests();  
             float getAverage();        
             float getAverage(float []);   

			 Student & operator=(const Student &);
};
What about the implementation?
Sorry...thank you for the quick responses...
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#include "Student.h"
#include <iostream>
#include <string>


using namespace std;

Student::Student()
{
     studentID = 0;
     studentName = " ";
     studentAvg = 0.0;
     testScores = new float [10];
}

Student::Student(int id, string name, float * tests)    //student class constructor
{
     studentID = id;
     studentName = name;
     testScores = new float [10];
         
     for(int i = 0; i < 10; i++)
     {
          testScores[i] = tests[i];
     }
     
     studentAvg = getAverage(testScores);
}

Student& Student::operator=(const Student & thatStudent)
{
	if(this != &thatStudent)
	{
		studentID = thatStudent.studentID;
		studentName = thatStudent.studentName;
		testScores = new float [10];
		for(int i = 0; i < 10; i++)
		{
			testScores[i] = thatStudent.testScores[i];
		}
     
		studentAvg = getAverage(testScores);
	}
	return *this;
}

Student::~Student()
{
     delete [] testScores;
}

Student::Student(const Student & student)  
{
     studentID = student.studentID;
     studentName = student.studentName;
     testScores = new float [10];

     for(int i = 0; i < 10; i++)
     {
          testScores[i] = student.testScores[i];
     }
     
     studentAvg = getAverage(testScores);

}


void Student::displayTests()
{          
     cout << "Test Scores: ";
     
     for (int i = 0; i < 10; i++)
     {
         cout << testScores[i] << " " ;
     }
}

float Student::getAverage()
{
      float sum = 0.0; 
      float average = 0.0; 
      
      for(int i = 0; i < 10; i++)
      {
           sum += testScores[i];
      }
      
      average = sum/10;
      return average;
}

float Student::getAverage(float tests[])
{
      float sum = 0.0; 
      float average = 0.0; 
      
      for(int i = 0; i < 10; i++)
      {
           sum += tests[i];
      }
      
      average = sum/10;
      return average;
}
SOLVED the garbage output problem...but I still can't figure out how to access the elements in the array for proper display...? thanks!!
Hmm... I'm stumped on problem 1. I don't see any reason why it would output something - there must be something you're missing.

From Problem #2, you have displayTests() returning nothing, but you expect it to return something when you give it to <<. I'd change your code to

in displayVector:
1
2
3
4
5
// ...
         cout << "Student Average: " << vect[count].getAverage() << endl;
         vect[count].displayTests();       
         cout << endl;
// ... 


in displayTests:
1
2
3
4
5
6
7
     cout << "Test Scores: ";
     
     for (int i = 0; i < 10; i++)
     {
         cout << testScores[i] << " " ;
     }
     cout << endl;


perfect! thank you!!
Topic archived. No new replies allowed.