Please Please Help me - reading class member data into an array using a class member function

Hello,

The programme I have developed so far does the following:
1. Writes to a file (route1.txt) the details of a planet - planet id, x, y and z coordinates using the function get_details()
2. Reads from the file using the fuinction generate_planet()
3. Outputs the data from the file onto the screen using report_planet()

In the function report_planet(), I want to read the the class data (planet id, x, y, and z coordinates) reported by report_planet() into an array. Once I have this array, I want to feed this array of planet data into my function total_route_length(). Can you advise me on how to do this successfully?

Here is the programme:

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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
#include <iostream>
#include <fstream>
#include <cmath>

using namespace std;

class planet
{
private:
	float planet_id, x, y, z;

public:
	planet ();
	planet (float, float, float, float);
	planet (const planet &copy_planet);
	~planet();
	void get_details(ofstream &fout);
	void generate_planet (ifstream &fin);
	planet report_planet ();
	float total_route_length (planet *first_planet, planet *second_planet);
};

planet::planet ()
{
	planet_id = 0.0;
	x = 0.0;
	y = 0.0;
	z = 0.0;
}

planet::planet (float id, float x_coord, float y_coord, float z_coord)
{
	planet_id = id;
	x = x_coord;
	y = y_coord;
	z = z_coord;
}

planet::planet (const planet &copy_planet)
{
	planet_id = copy_planet.planet_id;
	x = copy_planet.x;
	y = copy_planet.y;
	z = copy_planet.z;
}

planet::~planet()
{
	planet_id = 0.0;
	x = 0.0;
	y = 0.0;
	z = 0.0;
}

void planet::get_details (ofstream &fout)
{
	cout << "Enter the details for Planet " << endl;
	cout << "Enter the Planet's ID: ";
	cin >> planet_id;
	fout << planet_id << "\t";
	cout << "Enter the planet's x coordinate: ";
	cin >> x;
	fout << x << "\t";
	cout << "Enter the planet's y coordinate: ";
	cin >> y;
	fout << y << "\t";
	cout << "Enter the planet's z coordinate: ";
	cin >> z;
	fout << z << endl;
	cout << endl;
}

void planet::generate_planet (ifstream &fin)
{ 
	fin >> planet_id;
	fin >> x;
	fin >> y;
	fin >> z;
}

planet planet::report_planet ()
{
	planet *new_planet = new planet;
	cout << "Planet's ID: " << planet_id << endl;
	cout << "x coordinate: " << x << endl;
	cout << "y coordinate: " << y << endl;
	cout << "z coordinate: " << z << endl;
	cout << "\n";
	return *new_planet;
}

float planet::total_route_length (planet *first_planet, planet *second_planet)
{
	if ((first_planet -> planet_id)==1)
	{
		float base_distance = sqrt(pow((first_planet -> x) - 0,2) + pow((first_planet -> y) - 0,2) + pow((first_planet -> z) - 0,2));
		float result = base_distance + sqrt(pow((first_planet -> x) - (second_planet -> x),2) + pow((first_planet -> y) - (second_planet -> y),2) + pow((first_planet -> z) - (second_planet -> z),2));
		return result;
	}
	if ((second_planet -> planet_id)==1)
	{
		float base_distance = sqrt(pow(0 - (second_planet -> x),2) + pow(0 - (second_planet -> y),2) + pow(0 - (second_planet -> z),2));
		float result = base_distance + sqrt(pow((first_planet -> x) - (second_planet -> x),2) + pow((first_planet -> y) - (second_planet -> y),2) + pow((first_planet -> z) - (second_planet -> z),2));
		return result;
	}
	if ((first_planet -> planet_id)>1 && (second_planet -> planet_id)>1)
	{
		float result = sqrt(pow((first_planet -> x) - (second_planet -> x),2) + pow((first_planet -> y) - (second_planet -> y),2) + pow((first_planet -> z) - (second_planet -> z),2));
		return result;
	}
}

void main()
{
	int i, temp_size;
	float distance = 0;
	planet mars;
	planet the_red_one(mars);
	
	ofstream fout;
	fout.open ("route1.txt");
	cout << "Enter the number of planets: ";
	cin >> temp_size;
	fout << temp_size << endl;
	cout << endl;
	for (i=0; i<temp_size; i++)
		mars.get_details (fout);
	fout.close ();

	ifstream fin;
	fin.open ("route1.txt");
	fin >> temp_size;
	planet *the_planets = new planet [temp_size];
	for (i=0; i<temp_size; i++)
	{
		//int j=j+1;
		mars.generate_planet (fin);
		the_planets[i] = mars.report_planet ();
		//if (j>(temp_size-1)) 
			//j = 0;
		//distance = distance + mars.total_route_length (the_planets[i], the_planets[j]);		//pass pointer to planet to calculate distance between planets
		//if (j==0)
			//cout << "The Total Route Length is: " << distance << endl;
	}
	fin.close ();

	cin.sync ();
	cin.get ();
}
Last edited on
for the total_route_length() function:
calculate the distance from x,y,z [location] to next x,y,z [location], and add them together.
planetDist[0] + planetDist[1] ... in a loop.

for read data into array:
Here are 2 functions from Nehe, that load text files. They are in lesson 10 on his site at http://nehe.gamedev.net/default.asp The rest of the values are there for these 2 functions, along with the text file that they read from.
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
void readstr(FILE *f,char *string)
{
	do
	{
		fgets(string, 255, f);
	} while ((string[0] == '/') || (string[0] == '\n'));
	return;
}

void SetupWorld()
{
	float x, y, z, u, v;
	int numtriangles;
	FILE *filein;
	char oneline[255];
	filein = fopen("data/world.txt", "rt");				// File To Load World Data From

	readstr(filein,oneline);
	sscanf_s(oneline, "NUMPOLLIES %d\n", &numtriangles);

	sector1.triangle = new TRIANGLE[numtriangles];
	sector1.numtriangles = numtriangles;
	for (int loop = 0; loop < numtriangles; loop++)
	{
		for (int vert = 0; vert < 3; vert++)
		{
			readstr(filein,oneline);
			sscanf_s(oneline, "%f %f %f %f %f", &x, &y, &z, &u, &v);
			sector1.triangle[loop].vertex[vert].x = x;
			sector1.triangle[loop].vertex[vert].y = y;
			sector1.triangle[loop].vertex[vert].z = z;
			sector1.triangle[loop].vertex[vert].u = u;
			sector1.triangle[loop].vertex[vert].v = v;
		}
	}
	fclose(filein);
	return;
}

Thanks for your response Thazager! With your information, I have further increased my C++ repertoire.

In relation to this post, the question I would really like answered is: How do I implement a function that takes no argument but returns an array that contains new values for class objects?

In my particular code, I would like to achieve this with the report_planet() function; it takes no arguments but it should return an array that houses new values for planet_id, x, y and z. I would also like to keep the generate_planet() function as the only class member function that reads data from a file.

Is this possible? If it is, I would be grateful if someone could provide the code or even a helpful hint. Please help me!
Last edited on
I really need some advice... please please help!
Why can't you have it return an actual planet object?
Topic archived. No new replies allowed.