getting class to add data to an array

Hey ya'll, I have huge problem with my code; im trying to pass a number to a class function and have it add it to an array of that class. The problem I get is that when I display the array, it just gives me the memory address and not the data of the array. I was expecting it to display 12 0 0 0 0 0 ect.
any help would be a life-saver ;p

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
  #include <iostream>
using namespace std;

class stats
{
private: 
	double array[12];

public:
	
	void total()
	{
		for (int i =0; i <12 ; i++)
		{
			cout << array[0] <<endl;
		}
	}
	bool storeValue(double);
};

bool stats::storeValue(double num)
{
	int count =0; 
	while (array[count] != 0)
	{
		count ++;
	}
	array[count] = num;
	return true;
}

int main () 
{
	
	stats object;

	if (object.storeValue(12))
	{cout << "that worked\n";}
	object.total();
}
Hi bookLernin,

I'm afraid you have quite a few bugs in your code.
First, you array member is never initialized to something. You should create a constructor that set the whole array to 0 for example. If you don't know how to do it, let me know.

Second, in total(), you have a loop with i from 0 o 12 (not included), but you don't use i, you print out 12 times array[0]. I suppose this is not what you want.

Third, in your storeValue, what will hapen when your array will be full of numbers different that 0 ? You'll try to access something undefined and your program will crash.

Can you correct all this ?

Thanks for the quick response aleonard. Here is my code so far. I added a constructor, but i'm unsure as to how to go about setting the whole array equal to zero(if it doesn't automatically do that). I fixed the total() function. The big problem I cant wrap my head around is the storeValue function. I'm trying to get it to accept a value, store it in the next available element of the array. and return false when all elements have been used up. The code keeps returning false; my guess is that the array isn't initialized to zero. I thought as long as a space was available in the array it was automatically set to zero; thanks for any input you may have ;p




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
#include <iostream>
using namespace std;

class stats
{
private: 
	double array[12];

public:
	stats (double n=0)
	{
		if (storeValue(n) != true)
		{
			cout << "value not stored\n";
		}
	}
	void total()
	{
		for (int i =0; i <12 ; i++)
		{
			cout << array[i] <<endl;
		}
	}
	bool storeValue(double);
};

bool stats::storeValue(double num)
{
	int count =0; 
	while (array[count] != 0)
	{
		if (count >=12)
		{
			return false;
		}
		count ++;
	}
	array[count] = num;
	return true;
}

int main () 
{
	
	stats object(4);
	object.storeValue(5);
	object.total();

	
}
Hi again,

Good, it's improving ;) Don't forget the semi-colon at the end of your stats constructor and at the end of yout total() function.

In your constructor, you can simply have a loop that iterates through the 12 cells of your array and assign each cell to 0, like this:

1
2
3
for (int i = 0; i<12; i++) {
   array[i] = 0;
}


So that as soon as you create an object stats, its array is initialized to 0 (it is not done automatically as you noticed).

Then I think your code should work.
Wow, simple for loop helped tremendously. Thanks alot for your help!
Topic archived. No new replies allowed.