what is problem? i don't know ...

Write your question here.

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
 #include <iostream>
#include <string>
#include <fstream>
#include <vector>
const int N = 4;
using namespace std;
class ocean{
	string saxeli;
	double partobi;
public:
	ocean(){}
	ocean(ifstream &ifs){
		ifs >> saxeli >> partobi;
	}
	string get_s(){ return saxeli; }
	double get_p(){ return partobi; }
};
void print(vector<ocean>& x){
	for (int i = 0; i < N;i++)
	if (x[i].get_p() < 150)
		cout << "misi saxelia" << x[i].get_s() << endl
		<< "xolo misi partobia " << x[i].get_p() << endl;
}
void printfile(vector<ocean>c){
	ofstream ofs("Pasuxi.txt");
	double sum = 0;
	for (int i = 0; i < N; i++)
		sum += c[i].get_p();
	double average = 0;
	average = sum / 4.;
	ofs << "SASHUALO PARTOBI GAMOVIDA " << average;
}
int main(){
	vector<ocean> x(N);
	ifstream ifs("Ocean.txt");
	for (int i = 0; i < N; i++)
		ocean FN(ifs);
	print(x);
	printfile(x);
}
¿why do you think there is a problem?
main problem:

1
2
3
4
5
6
7
8
9
10
int main(){
	vector<ocean> x(N); // This actually creates N ocean objects
	ifstream ifs("Ocean.txt");
	// the next 2 lines repeatedly create (and destroy as they go out of scope), another N ocean objects and does nothing with them
	for (int i = 0; i < N; i++) 
		ocean FN(ifs); 
	// the next 2 lines work on the original, empty N objects!
	print(x); 
	printfile(x);
}


suggestion:
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
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
const int N = 4;
using namespace std;
class ocean{
	string saxeli;
	double partobi;
public:
	ocean(){}
	ocean(ifstream &ifs){
		ifs >> saxeli >> partobi;
	}
	string get_s() const { return saxeli; }
	double get_p() const{ return partobi; }
};
void print(const vector<ocean>& x){
	for (int i = 0; i < N;i++)
	if (x[i].get_p() < 150)
		cout << "misi saxelia " << x[i].get_s() << endl
		<< "xolo misi partobia " << x[i].get_p() << endl;
}
void printfile(const vector<ocean>& c){ 
	ofstream ofs("Pasuxi.txt");
	double sum = 0.0;
	for (int i = 0; i < N; i++)
		sum += c[i].get_p();
	double average = sum / 4.0;
	ofs << "SASHUALO PARTOBI GAMOVIDA " << average;
}
int main(){
	vector<ocean> x(N); // this may not work in newer compilers?
	ifstream ifs("Ocean.txt");
	for (int i = 0; i < N; i++) 
		x[i] = ocean(ifs); // this is not bounds checked!
	print(x);
	printfile(x);
}
Last edited on
Topic archived. No new replies allowed.