How to clear up rest of unused array???

Help, right now my total from my function comes out as like 15190923, which is way too high. It should be around 4200. I know that if I change it so that my for loop is (int i = 0; i < 36; i++) then it works but how can I clear up the unused portions of my array so that I can just keep it as it is (I want to use this function with other files that have more than 36 lines)? Thanks for the help!

Also my namespaces dont work I get errors when I compile any help there either??

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
81
82
83
84
85
#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:

	ifstream infile;
	
	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";

Cincinnati.count_animals_by_type();
}

void ZooClass::load_animals(string filename)
{

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 should probably do it in the constructor but since I don't know much about C++ classes I did it in load_animals.

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
void ZooClass::load_animals(string filename)
{
    
    for(int i=0;i<100;++i)
    {
        zoo[i].type =0; zoo[i].food=0;
    }
    
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++;
	}
	
}


Don't know about your namespace problem. Have you included the file with the summary_food_report function declaration?
Last edited on
Topic archived. No new replies allowed.