HELP WITH SIMPLE FUNCTION!? Please

Hello can someone please tell me what I'm doing wrong? I am trying to compute the total amount of food but for some reason it's coming out as 1517695209 which is way too much it should be around 4200.

Here is the data and my code so far:
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
1	40
1	45
1	40
1	45
2	30
2	28
2	32
3	55
3	60
3	65
4	80
4	85
4	90
5	120
5	110
5	105
6	240
6	290
6	325
7	220
7	220
7	220
8	300
8	325
9	200
9	200
9	200
9	210
10	40
10	42
10	44
10	44
10	40
10	25
10	25

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




class ZooClass
{
public:
	int compute_total_food();
	int total_food_by_type();
	int count_animals_by_type();
	int hungriest_animal();
	void load_animals(string filename);
	
	/*
	using namespace san_diego;
	{
		void summary_food_report();
	}
	
	using namespace cincinnati;
	{
		void summary_food_report();
	}
*/
	
private:
	
	struct animal
	{
		int type;
		int food;
	}  zoo[100];
	
};


int main()
{
ZooClass Cincinnati, San_Diego;

Cincinnati.load_animals("cincinnati.animals");

cout << "The total pounds of food needed for the Cincinnati Zoo is: " << Cincinnati.compute_total_food() << "\n";

}

void ZooClass::load_animals(string filename)
{
ifstream infile;
int ofst = 0;

	infile.open("cincinnati.animals");
	if ( infile.fail() )
	{
	cerr << "cannot open infile input file\n\n";
	exit(1);
    }

	while(infile >> zoo[ofst].type)
	{
		infile >> zoo[ofst].food;
		ofst++;
	}
	
}
	
int ZooClass::compute_total_food()
{
	int total = 0;
	
	for(int i = 0; i <= 100; i++)
	{
		total += zoo[i].food;
	}
		return(total);
}
You read in 35 values, but you then add up 101 values (which seems odd in itself, since your array only has 100 elements - I suspect you meant for(int i = 0; i < 100; i++). All the elements you have not written into will have some random number in them.

You must add up ONLY the elements you have stored a value in.
Last edited on
ah, my man..or woman. Thanks a lot! It's always the simplest answer!
How would I make it so that the unwritten elements are blank so that I can keep it at i < 100?
You misunderstand what (conventional, standard PC) memory is. There is no such thing as blank memory. Memory has a physical reality; it is real, it has a physical state that represents data, it can never be "blank".

The variables you use in your program ultimately are stored in physical memory, and thus they, too, can never be "blank".

When you allocate an array, every element in that array (you know where this is going) is ultimately stored in memory and can never be "blank". Every element in your array will have some value. There is nothing you can do about this. It is up to you to keep track of which elements you actually care about, and which elements you do not.

Since you're adding the elements, if you simply set every element to zero at the start, you'd not have to worry about adding elements you weren't using - they'd have a value of zero.
Last edited on
Thank you, Since you seem pretty smart can you help me with my other question? No one has answered but I really need help. Thanks for your time. Here's the link if you feel like helping out: http://www.cplusplus.com/forum/beginner/63414/
Topic archived. No new replies allowed.