A little help with my code

Can Someone help me with this problem , been breaking my head over it for a while now :)


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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
//Problem. The first line of the file "trees1.txt" holds the number of the trees in the park. 
//The following lines contain the names and heights of the trees in the park. 
//Read the data to the collection of objects Tree. Find out the height of the highest tree in the park.
//Count how many trees are higher than 30 meters. Find out the average of the heights of the trees. 
//Display the initial data and results on the screen. Organize the solving of the problem in the main() function.






#include<iostream>
#include<string>
#include<fstream>
#include<iomanip>

using namespace std ;

class trees
{
public:
	string name;
	double height;
};

void readdata(string fv, trees A[], int & nc);//Reading data from the file fv to the array
void writedata(string fv, trees A[],int & nc);//Writing data from the file fv to the array
void DisplayData(trees A[], int nc);// Displaying values from S(n) on the screen
int Max(trees A[], int nc); //Finding the maximal value in the array
int Aver(trees A[], int nc);//Finding the average of the height
int Many(trees A[], int nc); //Finding the number of objects that satisfy the condition
int main ()
{
	 trees A[100];
	 int nc; 
	 ofstream fr("Result.txt");
	 
	 readdata("trees1.txt", A, nc); 
	 writedata("Result.txt", A, nc); 
	 fr.open("Result.txt", ios::app); 
	 fr.setf(ios::fixed); fr.setf(ios::left); 

}





	 void readdata(string fv, trees A[], int & nc)
{
ifstream fd(fv.c_str()); 
fd >> nc; 
fd.ignore();
for (int i = 0; i < nc; i++)
{ getline(fd, A[i].name, ','); 
fd >> ws; fd >> A[i].height; 
fd.ignore();
} 
fd.close(); 
	 
}

	 void writedata(string fv, trees A[], int & nc) 

	 { 
	 ofstream fr(fv.c_str(), ios::app);
	 fr.setf(ios::fixed); fr.setf(ios::left); 
	 fr << "Number of Trees: " << nc << endl;
	 fr << "List of types of Trees:\n"; 
	 fr << "--------------------------------\n"; 
	 fr << "| Name            | Height     |\n";
	 fr << "--------------------------------\n";
	 for (int i = 0; i < nc; i++) 
	 { fr << "| " << setw(15) << A[i].name << " | " << setw(10) << A[i].height<< " | " << setprecision(2) <<endl; }
	 fr << "--------------------------------\n";
	 fr << "Average Height of Trees : " << setprecision(2)
<< Aver(A, nc) << " m" << endl;
	 fr<< "Height Of the Highest Tree in the Park : "<<setprecision(2)<<Max(A,nc)<<endl;
fr.close(); }

	 void DisplayData(trees A[], int nc)
	 {

		 cout<<"Number of Trees: " << nc << endl;
		 cout<<"List of types of Trees:\n";
		 cout<<"--------------------------------\n";
		 cout<<"| Name            | Height     |\n";
		 cout<<"--------------------------------\n";
		 for (int i = 0; i < nc; i++) 
	 { cout<< "| " << setw(15) << A[i].name << " | " << setw(10) << A[i].height<< " | " << setprecision(2) <<endl; }
	   cout<< "--------------------------------\n";
	 }


	 int Max(trees A[], int nc)
	 {
		  double A[100] ;
    double temp = 0;

    for(int i=0;i<nc;i++)
    {
        if(A[i].height>temp)
		temp=A[i].height;
    }


	 }
	 


	   int Aver(trees A[], int nc)
	   
 {
double sum = 0;
int count = 0;
for (int i=0; i<nc; i++)
	{
sum = sum + A[i].height;
count++;
}
	if(count>0)
return sum / count;
	else
		return 0.;
}
	   
	   

	


Result.txt
Number of Trees: 5
List of types of Trees:
--------------------------------
| Name            | Height     |
--------------------------------
| Maple           | 25.000000  | 
| Mango           | 22.00      | 
| Apple           | 35.00      | 
| Coconut         | 40.00      | 
| Palm            | 42.00      | 
--------------------------------
Average Height of Trees : 32 m


trees1.txt ------>INPUT
5
Maple, 25
Mango, 22
Apple, 35
Coconut, 40
Palm, 42
 
Last edited on
delete line 97.

i'd rename that Max() method to something more specific.
also you've told the compiler that method should return an integer but it's not returning anything at all at the moment.

that should do for now.
report some more specific issues when you next post.
Last edited on
Topic archived. No new replies allowed.