Trouble with my getFunctions in header

I'm working on assignment dealing with a class file. the constructor creates a dynamically allocated array of users choice of size. where i have set member functions for the different elements in the new array. I have no trouble with the storing and retrieving of element values, but when i come to my get functions to return the highest, lowest and average values it wants to display the first elements value; and "inf" for the average....some help would be greatly appreciated. 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
74
75
76
77
78
79
80
81
#ifndef NUMBER_H
#define NUMBER_H
#include<iostream>
using namespace std;

class Number
{
 private:
   int SIZE;
   float *ptr;
   float store;
   int retrieve;
   float highest;
   float lowest;
   float average;

 public:
   Number(int SIZE)
   {
    ptr = new float [SIZE];
   }

   ~Number()
    {
     cout << "deconstructor is running" << endl;
     delete [] ptr;
    }

   void storeNum(float store)
   {
    int elm;
    cout << "Which element do you wish to store your new number?: ";
    cin >> elm;
    ptr[elm] = store;
   }

   void retrieveNum(int retrieve)
   {
    cout << "value " << ptr[retrieve] << " is at element " << retrieve << endl;
   }

   void getHighest()
   {
    highest = ptr[0];
    for(int i = 1; i < SIZE; i++)
    {
     if(ptr[i] > highest)
     {
      highest = ptr[i];
     }
    }
    cout << "The highest value in the array is " << highest << endl;
   }

  float getLowest()
  {
   lowest = ptr[0];
   for(int i = 1; i < SIZE; i++)
   {
    if(ptr[i] < lowest)
     lowest = ptr[i];
   }
   cout << "The lowest value in the array is " << lowest << endl;
   return lowest;
  }

 float getAverage()
 {
  for(int i = 0; i < SIZE; i++)
  {
   average += ptr[i];
  }
  average /= SIZE;
  cout << "The avarage of all the values in the array is " << average << endl;
  return average;
 }

};

#endif
You never initialize your 'SIZE' member var.
Disch - Sorry i failed to mention that when the constructor is called that it accepts an int argument to declare the size of the array. so wouldn't 'SIZE' already be initialized if the constructor were to be used in the beginning? such as:
 
 Number myNum(5);
Last edited on
No, that's not how it works.

The 'SIZE' member declared on line 9 and the 'SIZE' parameter on line 18 are two completely different variables. Just giving the parameter the same name as your member does not make them the same (it just makes it a little confusing).

I'd give the two different names, then assign the member:

1
2
3
4
5
6
7
8
9
10
11
12
class Number
{
 private:
   int SIZE;
//...

 public:
   Number(int s) // <- change the name to 's'
   {
    SIZE = s; // <- now initialize your 'SIZE' member
    ptr = new float [SIZE];
   }
Sometimes I just talk myself out of things i already knew haha. Thanks for the clarification Disch, works perfectly now!
Topic archived. No new replies allowed.